0

Let me start right off the code:

<?php
class Father{
    function Father(){
        echo 'A wild Father appears..';
    }

    function live(){
        echo 'Some Father feels alive!';
    }
}

class Child{
    private $parent;
    function Child($p){
        echo 'A child is born :)';
    }

    function setParent($p){
        $parent = $p;
    }

    function dance(){
        echo 'The child is dancing, when ';
        $parent -> live();
    }
}

$p = new Father();
$p -> live();
$c = new Child($p);
$c -> dance();

?>

When running this I get an error on Line 24 saying "PHP Fatal error: Call to a member function live() on a non-object in ../test.php on line 24" I've searched the web for a while now and can't find a solution for this to work. Can someone help me with my poor understanding of php5?

2
  • 1
    Did you know that constructors should be named __construct instead of NameOfTheClass in PHP5? Commented Apr 15, 2011 at 23:58
  • No, I didn't. I'm just learning the language since I've got to write something in it for a project. Thanks for your advice :) Commented Apr 16, 2011 at 0:03

3 Answers 3

3

You need to use $this->parent->live() to access the member variable. Additionally, you have to assign the parent object to it.

class Child{
    private $parent;
    function __construct($p){
        echo 'A child is born :)';
        $this->parent = $p; // you could also call setParent() here
    }

    function setParent($p){
        $this->parent = $p;
    }

    function dance(){
        echo 'The child is dancing, when ';
        $this->parent -> live();
    }
}

Besides that, you should rename your constructor methods to __construct which is the suggested name in PHP5.

Sign up to request clarification or add additional context in comments.

1 Comment

Aah! That's good to know :) - 9 Minutes to go until this may become the solution ^^
2

You didn't call setParent in your constructor.
This will fix it:

function Child($p){
    echo 'A child is born :)';
    $this->setParent($p);
}

1 Comment

Usually getters/setters are for the external interface and internally accessing the field directly is better.
0

First, the preferred method to use constructor in PHP5 by using __construct keyword. When you access to class member you should to use $this, witch in your case you didn't when you tried to parent member.

function setParent($p){
        $parent = $p;
    }

Make it like this:

function setParent($p){
        $this->parent = $p;
    }

And this:

   function dance(){
        echo 'The child is dancing, when ';
        $parent -> live();
    }

To this:

   function dance(){
        echo 'The child is dancing, when ';
        $this->parent -> live();
    }

You will be end with this:

$p = new Father();
$p -> live();
$c = new Child();
$c -> setParent($p);
$c -> dance();

You don't need pass the parent to the child constructor as you will set it in setParent method.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.