Ste
Ste

Reputation: 1520

PHP Variable from extended class

I cant retrieve var from parent class:

class core {
    var $variable;
    var $test;

    function __construct() {}

    public function setVar($var) 
        $this->variable = $var;
    }
}

class test extends core {
    public function getVar() {
        return $this->variable;
        //also if i echo here i can't see !!!!
    }
}

$core = new core();
$core->setVar("ok");
$core->test = new test();
print $core->test->getVar();

Any help??

Upvotes: 0

Views: 1318

Answers (5)

Tgr
Tgr

Reputation: 28160

You do not seem to understand the difference between static and instance variables. Static variables exist only once and are accessible through any instance of the class, while for instance variables, each object has a different version of the variable, and setting the variable on another object does not effect it.

class Test {
    public $instance = 0;
    public static $static = 0;
    // ... getters, setters
}

// create two instances of class Test
$first = new Test();
$second = new Test();

$first->setInstance(1);
$first->setStatic(1);

$second->getInstance(); // 0
$second->getStatic(); // 1

So in your case, $core->variable and $core->test->variable are two different variables, and setting one has no effect on the other. If you declared $variable as static, then they would be the same variable.

Upvotes: 1

RobertPitt
RobertPitt

Reputation: 57268

The answer is simple, $variable does not hold a value,

you need to set a value like so:

$core = new core();
$core->test = new test();
$core->setVar("Something");

print $core->test->getVar(); //Something

Upvotes: 0

tacone
tacone

Reputation: 11441

Don't debug with echo or print !! Use var_dump () !

var_dump($core->test->getVar());

your mistake will become evident ! Also consider putting this line at the beginning of the script:

error_reporting( E_ALL );

Upvotes: 0

krtek
krtek

Reputation: 26597

Usually, class names in PHP starts with a capital letter : Core, Test, but this is only convention.

Then, you never assign a value to $variable, so when you call getVar(), the method returns nothing.

And like already said, var is an old syntax, you should now give a visibility to your variable with public, protected or private like in C++ or Java. In your case, you want to use public or protected.

Finally, your code doesn't make much sense. Usually getter and setter are defined at the same level (ie the same class) and there's no point in instantiating a Test object in a variable from Core. This create a dependency between Core and Test which is totally wrong from an OOP point of view.

Upvotes: 0

jonwayne
jonwayne

Reputation: 483

Try this:

<?php

class core{

    var $variable;
    var $test;

    function __construct() {}

    public function setVar($var) {
      $this->variable = $var;
    }

}


class test extends core {

    public function getVar() {

       return $this->variable;
       //also if i echo here i can't see !!!!
    }

}

$test = new test(); // will call core->__construct()
$test->setVar(5); // this isn't being called with your original code
print $test->getVar(); // now you can call this and get back "5"

?>

Upvotes: 0

Related Questions