i am new to php and is in need of some help to understand the aspects of php objects which i do not get. In class a i have made class b and c which extends a, but what i want to do is acess the public letters variable in class a from class c so in can get ot the function testb() from class c.
Any help is much appreciated guys
<?php
class a {
public $letters;
function __construct() {
$this->letters->b = new b();
$this->letters->c = new c();
}
}
class b extends a {
function __construct() {
echo "hello world from b ";
}
private function testb() {
echo "testing from b";
}
}
class c extends a {
function __construct() {
echo "hello world from c ";
$this->letters->b->testb();
}
}
$a = new a();
?>
the following script echos our "hello world from b" and "hello world from c" but it does not print out "testing from b"...
The reason you can't call testb
from within the class c
is that the method signature of testb
declares it as private
- meaning it can only be accessed within b
instances. Change the visibility to default (no modifier) or public
.
E.g.:
function testb() {
echo "testing from b";
}
Or:
public function testb() {
echo "testing from b";
}
Edit:
The problem is that you haven't called the super constructor from the constructor of c - the value of $this->b
is therefore not initialised. Change the constructor of c to:
function __construct() {
parent::__construct();
echo "hello world from c ";
$this->letters->b->testb();
}
Unfortunately, though, this will create an infinite loop where the constructor of a
is instantiating c
and the constructor of c
is calling up to the constructor of a
. Why do your letter classes even extend one another?
You also need to change the visibility of testb
within the b
class.
Ok, if you extend class c from b you can call the method otherwise not as you are extending it from class a not from c..
"c" extends "a" and doesn't inherit "b"s method
your example will return
Fatal error: Call to a member function testb() on a non-object
for
$this->letters->b->testb();
In your class a
you are constructing a new object of class c
.
This new object has no property $this->letters->b
, it just has an uninitialized property $this->letters
.
Note that the new object inherits the property $letters
from the class it extends, but it does not inherit any values of an instantiated a
object. So there is no $this->letters->b
and no function $this->letters->b->testb()
to call.
A gotcha of PHP Objects is that Inheritance is fudged (just a bit).. The constructor of a parent object is not implicitly called when you instantiate one of it's child objects. Instead you must explicitly call the Parent Object's constructor.
class b extends a {
...
public __construct() {
parent::__construct();
echo("Hello From B!");
}
...
}
In your Class c
constructor, $this->letters
is not an object and has not been initialized since the a
constructor has not been called.
If you're just playing around with objects, then ignore this part, but you're close to getting a circular hierarchy. If you add the changes that I suggested, I believe your code will explode if you try to instantiate either a B or a C object directly.