Answer the question
In order to leave comments, you need to log in
PHP property inheritance?
Good evening! At once I will make a reservation that I in OOP beginner. Faced such problem:
Class a
class a {<br/>
<br/>
var $boby;<br/>
<br/>
$b = new b();<br/>
<br/>
}<br/>
class b extends a {<br/>
<br/>
$this->boby="Текст";<br/>
<br/>
}<br/>
<?php<br/>
$a= new a(); <br/>
?><br/>
<br/>
<?php echo $a->body; ?><br/>
<br/>
Answer the question
In order to leave comments, you need to log in
Does the parent class create within itself an object of the class that is its child? And is it okay?
So you create a class of type a, so if you create a class b, then it will be taken from your definition in it.
In an instance of class a and there is no value in the property body , but it is in the property b of class a .
class a
{
var $body;
var $b;
function __construct()
{
$this->b = new b();
}
}
class b extends a
{
function __construct()
{
$this->body = 'text';
}
}
$a = new a();
echo $a->b->body;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question