2016-08-14 1 views
1

PHPMailer를 확장하는 사용자 정의 클래스가 있으며 send 함수를 재정의하려고합니다. 이것은 작동하는 것으로 보입니다.하지만 parent::send()이 활성 객체에서 작업 중이거나 무작위로 전송하는 경우 머리를 감싸는 데 문제가 있습니다. 기본적으로 parent::send()은 우리가 어떤 특정 객체를 처리하는지 어떻게 알 수 있습니까?PHPMailer 재정의 보내기

class Mailer extends PHPMailer 
{ 

    public function __construct() 
    { 
     $this->isSMTP();      
     $this->Host = 'smtp.gmail.com';  
     $this->SMTPAuth = true;    
     $this->Username = '';     
     $this->Password = '';     
     $this->SMTPSecure = 'ssl';    
     $this->Port = 465; 
    } 

    /** 
    * Overrides parent send() 
    * 
    * @return boolean 
    */ 
    public function send() { 
     if (!parent::send()) { 
      // do some stuff here 
      return false; 
     } else { 
      return true; 
     } 
    } 
} 

나는과 같이 인스턴스화 : 라이언의 말처럼, 어쨌든 작동됩니다 만,

$mail = new Mailer(); 
// create mailer stuff here 

$mail->send(); // <- How do I know this is acting on the $mail instance? 

답변

2

당신은 쉽게 테스트 할 수 있습니다. send 함수에서 검사를 반복 할 필요는 없으며, 부모 함수가 리턴하는 것을 되돌려 주면됩니다. 부모 생성자를 호출하여 재정의 할 때 수행하는 작업을 놓치지 않도록하고, 재정의 된 메소드 서명이 일치하는지 항상 확인해야합니다. 또한 465에서 SSL을 피하십시오. 그것은 1998 년 이후로 쓸모 없게되었습니다 :

class Mailer extends PHPMailer 
{ 

    public function __construct($exceptions = null) 
    { 
     parent::__construct($exceptions); 
     $this->isSMTP();      
     $this->Host = 'smtp.gmail.com';  
     $this->SMTPAuth = true;    
     $this->Username = '';     
     $this->Password = '';     
     $this->SMTPSecure = 'tls';    
     $this->Port = 587; 
    } 

    /** 
    * Overrides parent send() 
    * 
    * @return boolean 
    */ 
    public function send() { 
     echo 'Hello from my subclass'; 
     return parent::send(); 
    } 
} 
+0

Gotchya ... 나는 방금 전성기 체크가 필요하다고 생각합니다. 감사! – kylex