php: property inheritance on nested class structure
up vote
1
down vote
favorite
It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
Consider this example:
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
If I call in sequence
$classA = new ClassA();
$classA->SetA();
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
My expected behavior is to have...
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
...but I obtain instead...
Array
(
[0] => ClassB.construct
[1] => ClassB.Set
)
So, what's wrong on my side?
How can I add element from a Child to an array defined on Parent object?
php oop inheritance
add a comment |
up vote
1
down vote
favorite
It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
Consider this example:
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
If I call in sequence
$classA = new ClassA();
$classA->SetA();
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
My expected behavior is to have...
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
...but I obtain instead...
Array
(
[0] => ClassB.construct
[1] => ClassB.Set
)
So, what's wrong on my side?
How can I add element from a Child to an array defined on Parent object?
php oop inheritance
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
Consider this example:
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
If I call in sequence
$classA = new ClassA();
$classA->SetA();
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
My expected behavior is to have...
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
...but I obtain instead...
Array
(
[0] => ClassB.construct
[1] => ClassB.Set
)
So, what's wrong on my side?
How can I add element from a Child to an array defined on Parent object?
php oop inheritance
It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
Consider this example:
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
If I call in sequence
$classA = new ClassA();
$classA->SetA();
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
My expected behavior is to have...
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
...but I obtain instead...
Array
(
[0] => ClassB.construct
[1] => ClassB.Set
)
So, what's wrong on my side?
How can I add element from a Child to an array defined on Parent object?
php oop inheritance
php oop inheritance
asked Nov 14 at 8:19
Stefano Radaelli
480627
480627
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:
Class ClassB extends ClassA {
function __construct() {
parent::__construct();
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
And, at most, you'll get this
Array
(
[0] => ClassA.construct
[2] => ClassB.construct
[3] => ClassB.Set
)
as SetA()
is never invoked
When you invoked the sequence you described, $classA
and $classB
are two different instances, so you will never get what you expect.
To get what you want, you need to do this:
$classB = new ClassB();
$classB->SetB();
$classB->SetA();
print_r($classB->property);
add a comment |
up vote
3
down vote
You misunderstand how inheritance works in general: $classA
is an instance of ClassA
and has nothing to do with the instance $classB
of ClassB
you have generated.
An instance of ClassB
inherits all public and protected properties and methods of ClassA
but as long as you don't use them, you will not see them.
And all instances, whether from ClassA
or from ClassB
, are unrelated to each other, they only have the same "template" but each has its own property values.
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter inClassA
to set a private property. However, the property is still specific for that instance ofClassB
, it does not change anything in other objects ofClassA
orClassB
.
– jeroen
Nov 14 at 8:36
add a comment |
up vote
1
down vote
That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB
? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions
2
Also you need to callSetA()
somewhere in your class B instance to get[1] => ClassA.Set
in your array.
– Daniyal Nasir
Nov 14 at 8:23
add a comment |
up vote
1
down vote
If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.
You Updated Code
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
$this->SetA();
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
parent::__construct();//invoke parent constructor
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
Expected result:
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
When parent::__construct();
invokes then it maintains $property
array variable for child class also.
Note: As we know OOPS concept, every object has a different instance.
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:
Class ClassB extends ClassA {
function __construct() {
parent::__construct();
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
And, at most, you'll get this
Array
(
[0] => ClassA.construct
[2] => ClassB.construct
[3] => ClassB.Set
)
as SetA()
is never invoked
When you invoked the sequence you described, $classA
and $classB
are two different instances, so you will never get what you expect.
To get what you want, you need to do this:
$classB = new ClassB();
$classB->SetB();
$classB->SetA();
print_r($classB->property);
add a comment |
up vote
1
down vote
accepted
In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:
Class ClassB extends ClassA {
function __construct() {
parent::__construct();
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
And, at most, you'll get this
Array
(
[0] => ClassA.construct
[2] => ClassB.construct
[3] => ClassB.Set
)
as SetA()
is never invoked
When you invoked the sequence you described, $classA
and $classB
are two different instances, so you will never get what you expect.
To get what you want, you need to do this:
$classB = new ClassB();
$classB->SetB();
$classB->SetA();
print_r($classB->property);
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:
Class ClassB extends ClassA {
function __construct() {
parent::__construct();
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
And, at most, you'll get this
Array
(
[0] => ClassA.construct
[2] => ClassB.construct
[3] => ClassB.Set
)
as SetA()
is never invoked
When you invoked the sequence you described, $classA
and $classB
are two different instances, so you will never get what you expect.
To get what you want, you need to do this:
$classB = new ClassB();
$classB->SetB();
$classB->SetA();
print_r($classB->property);
In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:
Class ClassB extends ClassA {
function __construct() {
parent::__construct();
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
And, at most, you'll get this
Array
(
[0] => ClassA.construct
[2] => ClassB.construct
[3] => ClassB.Set
)
as SetA()
is never invoked
When you invoked the sequence you described, $classA
and $classB
are two different instances, so you will never get what you expect.
To get what you want, you need to do this:
$classB = new ClassB();
$classB->SetB();
$classB->SetA();
print_r($classB->property);
answered Nov 14 at 8:25
kitensei
1,33512354
1,33512354
add a comment |
add a comment |
up vote
3
down vote
You misunderstand how inheritance works in general: $classA
is an instance of ClassA
and has nothing to do with the instance $classB
of ClassB
you have generated.
An instance of ClassB
inherits all public and protected properties and methods of ClassA
but as long as you don't use them, you will not see them.
And all instances, whether from ClassA
or from ClassB
, are unrelated to each other, they only have the same "template" but each has its own property values.
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter inClassA
to set a private property. However, the property is still specific for that instance ofClassB
, it does not change anything in other objects ofClassA
orClassB
.
– jeroen
Nov 14 at 8:36
add a comment |
up vote
3
down vote
You misunderstand how inheritance works in general: $classA
is an instance of ClassA
and has nothing to do with the instance $classB
of ClassB
you have generated.
An instance of ClassB
inherits all public and protected properties and methods of ClassA
but as long as you don't use them, you will not see them.
And all instances, whether from ClassA
or from ClassB
, are unrelated to each other, they only have the same "template" but each has its own property values.
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter inClassA
to set a private property. However, the property is still specific for that instance ofClassB
, it does not change anything in other objects ofClassA
orClassB
.
– jeroen
Nov 14 at 8:36
add a comment |
up vote
3
down vote
up vote
3
down vote
You misunderstand how inheritance works in general: $classA
is an instance of ClassA
and has nothing to do with the instance $classB
of ClassB
you have generated.
An instance of ClassB
inherits all public and protected properties and methods of ClassA
but as long as you don't use them, you will not see them.
And all instances, whether from ClassA
or from ClassB
, are unrelated to each other, they only have the same "template" but each has its own property values.
You misunderstand how inheritance works in general: $classA
is an instance of ClassA
and has nothing to do with the instance $classB
of ClassB
you have generated.
An instance of ClassB
inherits all public and protected properties and methods of ClassA
but as long as you don't use them, you will not see them.
And all instances, whether from ClassA
or from ClassB
, are unrelated to each other, they only have the same "template" but each has its own property values.
edited Nov 14 at 8:37
answered Nov 14 at 8:23
jeroen
81.5k1698122
81.5k1698122
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter inClassA
to set a private property. However, the property is still specific for that instance ofClassB
, it does not change anything in other objects ofClassA
orClassB
.
– jeroen
Nov 14 at 8:36
add a comment |
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter inClassA
to set a private property. However, the property is still specific for that instance ofClassB
, it does not change anything in other objects ofClassA
orClassB
.
– jeroen
Nov 14 at 8:36
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
– Stefano Radaelli
Nov 14 at 8:29
1
1
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in
ClassA
to set a private property. However, the property is still specific for that instance of ClassB
, it does not change anything in other objects of ClassA
or ClassB
.– jeroen
Nov 14 at 8:36
@StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in
ClassA
to set a private property. However, the property is still specific for that instance of ClassB
, it does not change anything in other objects of ClassA
or ClassB
.– jeroen
Nov 14 at 8:36
add a comment |
up vote
1
down vote
That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB
? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions
2
Also you need to callSetA()
somewhere in your class B instance to get[1] => ClassA.Set
in your array.
– Daniyal Nasir
Nov 14 at 8:23
add a comment |
up vote
1
down vote
That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB
? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions
2
Also you need to callSetA()
somewhere in your class B instance to get[1] => ClassA.Set
in your array.
– Daniyal Nasir
Nov 14 at 8:23
add a comment |
up vote
1
down vote
up vote
1
down vote
That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB
? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions
That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB
? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions
answered Nov 14 at 8:21
Nico Haase
2,43591932
2,43591932
2
Also you need to callSetA()
somewhere in your class B instance to get[1] => ClassA.Set
in your array.
– Daniyal Nasir
Nov 14 at 8:23
add a comment |
2
Also you need to callSetA()
somewhere in your class B instance to get[1] => ClassA.Set
in your array.
– Daniyal Nasir
Nov 14 at 8:23
2
2
Also you need to call
SetA()
somewhere in your class B instance to get [1] => ClassA.Set
in your array.– Daniyal Nasir
Nov 14 at 8:23
Also you need to call
SetA()
somewhere in your class B instance to get [1] => ClassA.Set
in your array.– Daniyal Nasir
Nov 14 at 8:23
add a comment |
up vote
1
down vote
If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.
You Updated Code
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
$this->SetA();
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
parent::__construct();//invoke parent constructor
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
Expected result:
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
When parent::__construct();
invokes then it maintains $property
array variable for child class also.
Note: As we know OOPS concept, every object has a different instance.
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
add a comment |
up vote
1
down vote
If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.
You Updated Code
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
$this->SetA();
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
parent::__construct();//invoke parent constructor
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
Expected result:
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
When parent::__construct();
invokes then it maintains $property
array variable for child class also.
Note: As we know OOPS concept, every object has a different instance.
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
add a comment |
up vote
1
down vote
up vote
1
down vote
If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.
You Updated Code
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
$this->SetA();
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
parent::__construct();//invoke parent constructor
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
Expected result:
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
When parent::__construct();
invokes then it maintains $property
array variable for child class also.
Note: As we know OOPS concept, every object has a different instance.
If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.
You Updated Code
Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
$this->SetA();
}
public function SetA() {
$this->property = "ClassA.Set";
}
}
Class ClassB extends ClassA {
function __construct() {
parent::__construct();//invoke parent constructor
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);
Expected result:
Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)
When parent::__construct();
invokes then it maintains $property
array variable for child class also.
Note: As we know OOPS concept, every object has a different instance.
edited Nov 14 at 9:21
answered Nov 14 at 8:32
Gufran Hasan
3,30331325
3,30331325
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
add a comment |
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
– Stefano Radaelli
Nov 14 at 9:13
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
– Gufran Hasan
Nov 14 at 9:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295703%2fphp-property-inheritance-on-nested-class-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown