Your Employer class extends your User class, but when you create the $user and $employer objects, they are separate objects and are not related.
Think of your objects as:
$employer = new Employer(); // You now have $employer object with the following properties: // $employer->company_name; // $employer->fname; // $employer->sname; $user = new User(); // You now have $user object with the following properties: // $user->company_name; $employer->company_name = "Company name is "; // You now have $employer object with the following properties: // $employer->company_name = 'Company name is '; // $employer->fname; // $employer->sname; echo $user->company_name; // You currently have $user object with the following properties: // $user->company_name; /* no value to echo! */
If you want to use inherited properties, it works something like this:
class User{ public $company_name; function PrintCompanyName(){ echo 'My company name is ' . $this->company_name; } } class Employer extends User{ public $fname; public $sname; } $employer = new Employer(); $employer->company_name = 'Rasta Pasta'; $employer->PrintCompanyName();
Farray
source share