Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-子类不是从父类继承的_Php_Oop - Fatal编程技术网

PHP-子类不是从父类继承的

PHP-子类不是从父类继承的,php,oop,Php,Oop,这是第3课PacktPub开始PHP的活动。据我所知,它应该创建Employee类的实例$markus。Employee类是BaseEmployee的子类,因此继承了BaseEmployee的所有内容。但是,如果我尝试使用calculateMonthlyPay()方法,会出现一个通知,并且程序无法正确运行。我正在使用PHP7+和PHPStorm IDE。以下是通知信息和代码: 通知讯息: 注意:未定义的属性:Employee::$salary,C:\Users\ed.PhpStorm2019.2

这是第3课PacktPub开始PHP的活动。据我所知,它应该创建Employee类的实例$markus。Employee类是BaseEmployee的子类,因此继承了BaseEmployee的所有内容。但是,如果我尝试使用calculateMonthlyPay()方法,会出现一个通知,并且程序无法正确运行。我正在使用PHP7+和PHPStorm IDE。以下是通知信息和代码:

通知讯息:

注意:未定义的属性:Employee::$salary,C:\Users\ed.PhpStorm2019.2\config\scratch。\scratch\u 3.php,第40行 PHP通知:未定义的属性:Employee::$salary,位于第40行的C:\Users\ed.PhpStorm2019.2\config\scratch\scratch_3.PHP中 月薪是零 进程已完成,退出代码为0

守则:

<?php
class BaseEmployee {
  private $name;
  private $title;
  private $salary;

  function __construct($name, $title, $salary){
    $this->name = $name;
    $this->title = $title;
    $this->salary = $salary;
  }

  public function setName($name){
    $this->name = $name;
  }

  public function setTitle($title){
    $this->title = $title;
  }

  public function setSalary($salary){
    $this->salary = $salary;
  }

  public function getName(){
    return $this->name;
  }

  public function getTitle(){
    return $this->title;
  }

  public function getSalary(){
    return $this->salary;
  }
}

class Employee extends BaseEmployee{
  public function calculateMonthlyPay(){
    return $this->salary / 12;
  }
}

$markus = new Employee("Markus Gray", "CEO", 100000);
echo "Monthly Pay is" . $markus->calculateMonthlyPay();

通知不是错误。通知旨在指出您可能想要或不想要调查的事情

在本例中,您声明了一个具有私有变量的基类。私有变量和方法仅对该类的对象可见


当您从基类派生时,您希望将变量声明为private或protected,将方法声明为protected或public,但是您的错误是试图直接访问派生类中的私有变量。您应该在子方法中使用$obj->getSalary()。

通知不是错误。通知旨在指出您可能想要或不想要调查的事情

在本例中,您声明了一个具有私有变量的基类。私有变量和方法仅对该类的对象可见


当您从基类派生时,您希望将变量声明为private或protected,将方法声明为protected或public,但是您的错误是试图直接访问派生类中的私有变量。您应该在子方法中使用$obj->getSalary()。

您将
$salary
定义为
private


这意味着继承类将无权访问它。如果
Employee
应该访问它,您需要将
$salary
定义为
protected
或使用
getSalary()
$salary
定义为
private


这意味着继承类将无权访问它。如果
Employee
应该访问它,您需要将
$salary
定义为
protected
或使用
getSalary()
私有属性不被继承。在您的情况下,
$salary
属性被定义为private,这意味着它只能在基类中访问。如果希望在子类中使用它,而不是在外部使用,则可以使用
protected

<?php
class BaseEmployee {
    private $salary; // <-- defined as private
}

class Employee extends BaseEmployee {
    public function calculateMonthlyPay() {
        return $this->salary / 12; // This will trigger notice
    }
}

// ---------------------

class BaseEmployee {
    protected $salary; // <-- defined as protected
}

class Employee extends BaseEmployee {
    public function calculateMonthlyPay() {
        return $this->salary / 12; // This will work
    }
}

私有属性不会被继承。在您的情况下,
$salary
属性被定义为private,这意味着它只能在基类中访问。如果希望在子类中使用它,而不是在外部使用,则可以使用
protected

<?php
class BaseEmployee {
    private $salary; // <-- defined as private
}

class Employee extends BaseEmployee {
    public function calculateMonthlyPay() {
        return $this->salary / 12; // This will trigger notice
    }
}

// ---------------------

class BaseEmployee {
    protected $salary; // <-- defined as protected
}

class Employee extends BaseEmployee {
    public function calculateMonthlyPay() {
        return $this->salary / 12; // This will work
    }
}

不需要打开它,有一个getter
getSalary
。我保留了
private$salary与BaseEmployee类相同,并更改
返回$this->salary/12
to
return$this->getSalary()/12
在calculateMonthlyPay方法中,它成功了!非常感谢。不需要打开它,这里有一个getter
getSalary
与BaseEmployee类相同,并更改
返回$this->salary/12
to
return$this->getSalary()/12
在calculateMonthlyPay方法中,它成功了!非常感谢。请参考,大部分通知将很快升级为PHP中的警告或错误。不要忽视通知!我没有说要忽略它们,我说它们是信息性的。学习一门语言的工作原理是无可替代的。感谢您对注意事项和范围修饰符的解释。该代码来自教科书,他们尚未解释“受保护”。非常感谢!请参考,大部分通知将很快升级为PHP中的警告或错误。不要忽视通知!我没有说要忽略它们,我说它们是信息性的。学习一门语言的工作原理是无可替代的。感谢您对注意事项和范围修饰符的解释。该代码来自教科书,他们尚未解释“受保护”。非常感谢!非常感谢您的解释!我把它改成了私人的,它成功了!非常感谢您的解释!我把它改成了私人的,它成功了!