ENV
PHP 5.4
如何在php中调用constructure中的函数?它可能看起来像一个变量函数或指针函数。以下代码在function $EmailAddress
处获得sytax错误。如何纠正:
function __construct($username,$password,$EmailAddress,$mailserver='localhost',$servertype='imap',$port='143',$ssl = true) {
if($servertype=='imap') {
if($port=='')
$port='143';
$strConnect='{'.$mailserver.':'.$port.'/imap/ssl/novalidate-cert}';
} else {
$strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
//echo $port;
$this->server = $strConnect;
$this->username = $username;
$this->password = $password;
$this->email = $EmailAddress;
}
//Connect To the Mail Box
function $EmailAddress {
$this->marubox=@imap_open($this->server,$this->username,$this->password);
if(!$this->marubox) {
echo "Error: Connecting to mail server"; exit;
}
}
修改
也不起作用。
$this->email = "EmailAddress";
}
//Connect To the Mail Box
function EmailAddress() {
$this->marubox=@imap_open($this->server,$this->username,$this->password);
if(!$this->marubox) {
echo "Error: Connecting to mail server"; exit;
}
}
答案 0 :(得分:1)
您的代码中存在逻辑错误。您必须更改该函数的名称并按此调用它。
function __construct($username,$password,$EmailAddress,$mailserver='localhost',$servertype='imap',$port='143',$ssl = true) {
if($servertype=='imap') {
if($port=='')
$port='143';
$strConnect='{'.$mailserver.':'.$port.'/imap/ssl/novalidate-cert}';
} else {
$strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
//echo $port;
$this->server = $strConnect;
$this->username = $username;
$this->password = $password;
$this->email = $EmailAddress;
// Call a function like this.
$this->setupEmail();
}
//Connect To the Mail Box
private function setupEmail() {
$this->marubox=@imap_open($this->server,$this->username,$this->password);
if(!$this->marubox) {
echo "Error: Connecting to mail server"; exit;
}
}
答案 1 :(得分:1)
尝试,
function __construct($username,$password,$EmailAddress,$mailserver='localhost',$servertype='imap',$port='143',$ssl = true) {
if($servertype=='imap') {
if($port=='')
$port='143';
$strConnect='{'.$mailserver.':'.$port.'/imap/ssl/novalidate-cert}';
} else {
$strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
//echo $port;
$this->server = $strConnect;
$this->username = $username;
$this->password = $password;
$this->email = $EmailAddress;
$this->connect_imap(); // call the connect method
}
//Connect To the Mail Box
function connect_imap() {
$this->marubox=@imap_open($this->server,$this->username,$this->password);
if(!$this->marubox) {
echo "Error: Connecting to mail server"; exit;
}
}
如果要动态调用该函数,请尝试
class test{
private $func;
public function __construct($functionName){
$this->func = $functionName;
$this->{$this->func}();
}
public function my_func(){
echo "Hello World";
}
}
$r = new test('my_func');