我想我无法正确扩展PHP类

时间:2014-02-13 08:45:27

标签: php extends

家伙!我现在正在练习课程,但我觉得我真的很想念。

我首先有一些变量:

$username = "antonradev";
$name = "Anton Radev";
$email = "antonradev@example.com";
$profession = "Designer";
$job_title = "Web Design Manager";
$job_location = "Sofia";

我的父班:

class User {
    public $username;
    public $name;
    public $email;

    public function __construct($username, $name, $email) {
        $this->username = $username;
        $this->name = $name;
        $this->email = $email;
    }

}

在此之后我像这样延伸:

class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {
        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}

我创建了一个实例:

$user_professional = new User_Professional($username, $name, $email, $profession, $job_title, $job_location);

我正在尝试打印一些数据:

print "The employee username is: " . $user_professional->username;

但没有任何事情发生。它空了没有错误:

员工用户名为:

然后我做了一些更改,我正在尝试打印其他属性:

print "The employee`s job title is: " . $user_professional->user_job_title;

但是我从父类中获取数据。它打印错误的属性:

该员工的职位是:Anton Radev

这是正常的吗?我的错误在哪里?我无法处理它。谢谢!

4 个答案:

答案 0 :(得分:2)

您必须调用User类的构造函数并在其中传递3个参数$username, $name, $email

User类构造函数:

public function __construct($username, $name, $email) {
    $this->username = $username;
    $this->name = $name;
    $this->email = $email;
}

User_Professional类构造函数:

public function __construct($username, $name, $email, $user_profession, $user_job_title, $user_work_location) {
    parent::__construct($username, $name, $email);

    $this->user_profession = $user_profession;
    $this->user_job_title = $user_job_title;
    $this->user_work_location = $user_work_location;
}

答案 1 :(得分:1)

class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {

        // Need to pass the usename, name, email
        parent::__construct($username, $name, $email);

        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}

替代

class User {
    public $username;
    public $name;
    public $email;

    public function __construct() {
        $this->username = "antonradev";
        $this->name = "Anton Radev";
        $this->email = "antonradev@example.com";
    }

}

并在您的子课程中使用,如

class User_Professional extends User {

    public $user_profession;
    public $user_job_title;
    public $user_work_location;

    public function __construct($user_profession, $user_job_title, $user_work_location) {

        parent::__construct();

        $this->user_profession = $user_profession;
        $this->user_job_title = $user_job_title;
        $this->user_work_location = $user_work_location;
    }

}

答案 2 :(得分:0)

您正在覆盖您的父母构造函数。你需要打电话。

class User_Professional extends User {

  public $user_profession;
  public $user_job_title;
  public $user_work_location;

  public function __construct($user_profession, $user_job_title, $user_work_location) {
    parent::__constructor($username, $name, $email);

    $this->user_profession = $user_profession;
    $this->user_job_title = $user_job_title;
    $this->user_work_location = $user_work_location;
  }
}

答案 3 :(得分:0)

在这种情况下,您应该使用compositionmixin而不是继承。