E
E
enchikiben2011-02-28 21:33:19
PHP
enchikiben, 2011-02-28 21:33:19

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 inherited from a
class b extends a {<br/>
<br/>
$this-&gt;boby=&quot;Текст&quot;;<br/>
<br/>
}<br/>

I connect all this to the a.php file
&lt;?php<br/>
 $a= new a(); <br/>
?&gt;<br/>
<br/>
&lt;?php echo $a-&gt;body; ?&gt;<br/>
<br/>

If the property is set in class a, then it is displayed, and if it is defined in class b, then it is not displayed.
How to overcome this please tell me.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
Hint, 2011-02-28
@Hint

Does the parent class create within itself an object of the class that is its child? And is it okay?

O
Oleg Matrozov, 2011-02-28
@Mear

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.

D
Dzuba, 2011-02-28
@Dzuba

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 question

Ask a Question

731 491 924 answers to any question