PHP子类访问父变量问题

12 php oop

我上了这堂课:

Class Username {
protected $id;
protected $username;
protected $contact_information;

     private __construct($id) {
       $this->id = (int) $id; // Forces it to be a string
       $contact_information = new ContactInformation($this->id);
     }
}

Class ContactInformation extends Username {
    protected $mobile;
    protected $email;
    protected $nextel_id;
    .....
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:我想访问ContactInformation上的$ id和$ username(以及许多其他变量),但是parent ::或$ this->不起作用,看起来每次我都做"new ContactInformation ...." PHP创建一个"新用户名".有没有机会从用户名访问CURRENT值?

谢谢

Pat*_*and 12

为什么Username构造函数是私有的?如果您的意思是无法创建用户名,请将Username类设为abstract.另外,请勿从父类中创建新的联系信息.这是另一种方式:

abstract class Username {
   protected $id;
   protected $username;

   public __construct($id) {
      $this->id = (int) $id; // Forces it to be a string
   }
}

class ContactInformation extends Username {
    protected $mobile;
    protected $email;
    protected $nextel_id;
    public __construct($id, $mobile, $email, $nextel_id) {
       parent::__construct($id)
       $this->mobile = $mobile;
       ....
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您不是直接实例化用户名(现在是不可能的),而是创建一个ContactInformation.然后,ContactInformation在其自己的构造函数中调用Username构造函数.


Jam*_*col 8

parent ::方法仅用于访问已在子类中重写的父方法,或者用于以下静态变量:

class Base
{
    protected static $me;

    public function __construct ()
    {
        self::$me = 'the base';
    }

    public function who() {
        echo self::$me;
    }
}

class Child extends Base
{
    protected static $me;

    public function __construct ()
    {
        parent::__construct();
        self::$me = 'the child extends '.parent::$me;
    }

    // until PHP 5.3, will need to redeclare this
    public function who() {
        echo self::$me;
    }
}

$objA = new Base;
$objA->who(); // "the base"

$objB = new Child;
$objB->who(); // "the child extends the base"
Run Code Online (Sandbox Code Playgroud)

你可能想要一个合适的子类.不要在基类的构造函数中创建子类,这会将各种OOP最佳实践颠倒(松散耦合等),同时还会创建无限循环.(new ContactInformation()调用Username构造函数,该构造函数创建一个新的ContactInformation(),其中...).

如果你想要一个子类,像这样:

/**
 * Stores basic user information
 */
class User
{
    protected $id;
    protected $username;

    // You could make this protected if you only wanted
    // the subclasses to be instantiated
    public function __construct ( $id )
    {
        $this->id = (int)$id; // cast to INT, not string

        // probably find the username, right?
    }
}

/**
 * Access to a user's contact information
 */
class ContactInformation extends User
{
    protected $mobile;
    protected $email;
    protected $nextel;

    // We're overriding the constructor...
    public function __construct ( $id )
    {
        // ... so we need to call the parent's
        // constructor.
        parent::__construct($id);

        // fetch the additional contact information
    }
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用委托,但是ContactInformation方法不能直接访问Username属性.

class Username
{
    protected $id;
    protected $contact_information;

    public function __construct($id)
    {
        $this->id = (int)$id;
        $this->contact_information = new ContactInformation($this->id);
    }
}

class ContactInformation // no inheritance here!
{
    protected $user_id;
    protected $mobile;

    public function __construct($id)
    {
        $this->user_id = (int)$id;
        // and so on
    }
}
Run Code Online (Sandbox Code Playgroud)