7
class User{
    public $company_name;
}

class Employer extends User{
    public $fname;
    public $sname;
}

this is the test.php i have created. I have included the class file.

$employer = new Employer();
$user = new User();
$employer->company_name = "Company name is ";
echo $user->company_name;

When i print the name nothing happens, please let me know what is the wrong with my code.

2
  • 1
    You got everything right, just are printing out the wrong value, I think. Commented Mar 4, 2011 at 22:15
  • 1
    Please use a more descriptive subject. Commented Mar 4, 2011 at 22:21

5 Answers 5

16

Your Employer class extends your User class but when you create your $user and $employer objects they are separate entities and unrelated.

Think of your objects like this:

$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 more 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();  //echoes 'My company name is Rasta Pasta.'
Sign up to request clarification or add additional context in comments.

5 Comments

in php two different class the separate entities meaning even if you call one as super and other class as sub class.
Great explaination. The OP obviously has it in his head that he is correct.
@user630320 I can see why you are misunderstanding this, but please be sure that you are. Your confusion lies in creating 2 different objects of different types. The way you have written your code, $employer is of class Employer which inherits class User. However, $user is of class $User which has absolutely nothing to do with your $employer object.
Thank you, I'm justing getting into php programming, mostly i have done coding in java. Thank you for your answer.
@user630320 Just for kicks, try and recode your above PHP classes as Java classes and I am sure you would have the exact same problem.
4

Don't confuse your variables, $user and $employer, with classes. $user is an instance of class User, and $employer is an instance of class Employer, but they are separate variables.

Comments

3

You never set $user->company_name.

echo $employer->company_name;

2 Comments

because user is super class and employer is sub class which means it can use the super class attribute. i dont have to set $user->company_name because i have already set the $employer->company_name.
that's not how it works, PHP passes object instances by reference, not the object classes themselves. These are two separate, unrelated instances. If you set a concrete name in the class definition, then all the inheriting classes would have it by default.
3

You didn't assign something to $company_name of object $user; only to $employer.

1 Comment

because user is super class and employer is sub class which means it can use the super class attribute. i dont have to set $user->company_name because i have already set the $employer->company_name.
2

You just have to echo

 $employer->company_name;

or set

 $user->company_name

to some value.

You do not have to create an instance of the parent class to work with a child class. In this case, the $employer inherits company_name from the User class.

2 Comments

I wont to set the values with $employer but i also wont to access from $user can this be done. you now like java where sub class can access the super class attribute
hello. see response above that the objects are unrelated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.