0

This is the activity in the lesson 3 of Beginning PHP by PacktPub. From what I understand, it should create an instance $markus of the Employee class. The Employee class is a child of BaseEmployee and therefore inherits everything from BaseEmployee. However, if i try to use the method calculateMonthlyPay(), there is a notice and the program does not run correctly. I am using PHP7+ and PHPStorm IDE. here is the notice message and the code:

notice message:

Notice: Undefined property: Employee::$salary in C:\Users\ed.PhpStorm2019.2\config\scratches.\scratch_3.php on line 40 PHP Notice: Undefined property: Employee::$salary in C:\Users\ed.PhpStorm2019.2\config\scratches\scratch_3.php on line 40 Monthly Pay is0 Process finished with exit code 0

the code:

<?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(); 
0

3 Answers 3

2

You defined $salary as private

This means the inheriting class won't have access to it. If Employee should access it, you need to define $salary as protected or use getSalary()

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for the explanation! I changed it to private and it worked!
0

Private properties are not inherited. In your case your $salary property is defined as private, which means it will be accessible only in the base class. If you want it to be used in the child class, but not outside then you can use 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 } } 

2 Comments

No need to open it up, there's a getter getSalary.
i kept private $salary; as it is in the BaseEmployee class and changd return $this->salary / 12; to return $this->getSalary() / 12; in the calculateMonthlyPay method and it worked! thank you!!!
0

A notice is not an error. Notices are meant to point out things you may or may not want to investigate.

In this case, you declared a base class that had private variables. Private variables and methods are only visible to objects of that class.

When you derive from a base class, you want the variables to be declared as either private or protected, and the methods to be protected or public, however your mistake was trying to access a private variable directly in the derived class. You should have instead used $obj->getSalary() inside your child method.

3 Comments

For your information, most of the notice will soon be upgraded to warnings or errors in PHP. Do not ignore notices!
I didn't say to ignore them, I said they were informational. There is no substitution for actually learning how a language works.
Thank you for the explanation on notices and scope modifiers. This code is from a textbook and they have not explained "protected" yet. Much appreciated!