dragonhong641016 2012-07-08 01:39
浏览 29

PHP - 我可以创建一个除了通过另一个类之外无法实例化的类吗?

I'm trying to have a child class from the Exception class just to handle errors and emit the proper error message given the error code. I changed my original code and made it more simple just to illustrate my problem.

Maybe it's not possible, but I don't want the InvalidEmailException class to be instantiated by the script. I just want it to be used by the Subscribe class if it's necessary (errors are found). Why would I want to do this anyway? Doesn't matter I'm just trying to understand how classes work.

/* Child class from the parent Exception class to handle errors 
 * pertinent to users being subscribed
 */
class InvalidEmailException extends Exception{
   private $error_code;
   private $email;
   function __construct($error_code, $email){
      $this->error_code = $error_code;
      $this->email = $email;
      $this->notifyUser();
   }
   function notifyUser(){
        if($this->error_code == 2):
         echo "<p>Invalid email: <em>{$this->email}</em></p>";
      endif;
   }
}

// Initial class to subscribe a user with the try catch checks
class Subscribe{
   private $email;
   private $error_code = 0;
   function __construct($email){
      $this->email = $email;
      $this->validateEmail();
   }

   private function validateEmail(){
      try{
         if($this->email == ''):
            throw new Exception('<p>Error: empty email address.</p>');
         else:
            if($this->email == 'invalid test'){
                    $this->error_code = 2;
               throw new InvalidEmailException($this->error_code, $this->email);
            }elseif($this->error_code == 0){
               // Go to method to subscribe a user if the error code remains zero
               $this->subscribeUser();
            }
         endif;
      }catch(Exception $e){
         echo $e->getMessage();
      }
   }

   private function subscribeUser(){
      echo $this->email.' added to the database!';
   }
}

/* 
 * Script to use the Subscribe class, which would call 
 * the InvalidEmailException class if needed
 */
$email = 'invalid test'; // This could later on be used through the $_POST array to take an email from a form
$subscribe = new Subscribe($email); // Works well.
$test = new InvalidEmailException('2', 'a@b.c'); // Also works.  I want this to throw an error.
  • 写回答

3条回答 默认 最新

  • doulangdang9986 2012-07-08 02:30
    关注

    I think your best option would be to use dependency injection http://en.wikipedia.org/wiki/Dependency_injection. It's a bit advanced but I think it would help in the long run..

    Essentially, you could create a common interface with methods getting the data you need, and make the constructor for your exception class have a parameter of type of the interface. Then have the subscribe class implement the interface, which would allow it to be passed in to the constructor of the exception.

    You could also just pass in the subscribe object to the constructor of the exception class directly as the parameter and bypass the interface usage. However, the interface allows you to have multiple classes all use that interface and those common methods and they could all construct your exception class if you used that pattern. And you would also know how to interact with those classes as they use the same common design.

    This doesn't completely prevent you from constructing the exception from outside the class and in the script, however you will have needed to construct the class that implements the interface first (or just the class itself, depending on which way you go), which is a good way to restrict the behavior.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图