views:

77

answers:

7

I am trying to set a value from a method of a class and trying to get it in another method. The example code is as below. I think the below kind of set/get works in a Java class. Googled out but still could not find a relevant solution.

Searched: http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=how+to+share+data+across+functions+in+PHP&aq=f&aqi=&aql=&oq=&gs_rfai= http://stackoverflow.com/questions/2707442/pass-variable-number-of-variables-to-a-class-in-php http://stackoverflow.com/questions/1750837/php-call-member-variables-off-a-class-within-static-method

Thanks in advance.

<?php
class MyClass
{
    public $cons;

    function showConstant() {
        $this->setConstant(100); /* assign value to variable here */
        $this->showConstantGetter();
    }

    /* setter */
    function setConstant($aCons) {
        $cons  = $aCons;
    }

    /* getter */
    function getConstant() {
        return $cons;
    }


    function showConstantGetter() {
        echo "<br>getting const : ".$this->getConstant(); /* use the variable's value in this method here */
    }

}

$classname = "MyClass";
$class = new MyClass();
$class->showConstant();

?>
+10  A: 

You must use $this->cons instead of $cons, that's all.

nikic
+1, sad but true. every time that i go back to code some PHP i forget this... not so intuitive ;)
Zied
Yes, it's very intuitive. That's how objects work. Every language with dynamic object variables requires you to qualify access. Only compiled languages like C++ and Java let you access members without qualifying, but then you might accidentaly "shadow" a member variable. Excplicitly accessing the object is the only way to be unambigous.
gnud
"intuitive" is only a matter of habit. If you programed C++ your whole life and now start using PHP it isn't intuitive obviously.
nikic
Oops sorry, I do not get the values still: /* setter */ function setConstant($aCons) { $this->cons = $aCons; } /* getter */ function getConstant() { $this->cons; }
oneworld
You forgot the echo in the getConstant
Xeross
A: 

To access instance variables in PHP, you need to prefix them with $this->.

In your example:

    function getConstant() {
        return $this->cons;
    }

To access a class (or static) variable, you use self:: instead of $this->.

Hollance
Class variables are `$this->var;`Static variables are `self::$var;`
Treffynnon
@Treff "class variable" means "static variable"
Artefacto
@Artefacto never heard of that terminology solely referring to static class properties. http://www.php.net/manual/en/language.oop5.properties.php
Treffynnon
@Treff See http://en.wikipedia.org/wiki/Field_(computer_science%29
Artefacto
+3  A: 

Nearly good but:

function setConstant($aCons) {
        $this->cons  = $aCons;
    }

    /* getter */
    function getConstant() {
        return $this->cons;
    }
Tomasz Struczyński
A: 

Tried with $this-> operator as mentioned here. I get this error,

Fatal error: Cannot access empty property in C:\xampp\htdocs\varTest.php on line 13 in setter ie., $this->$cons = $aCons;

If I remove in line 13 it comes up with the same error in line 18 in getter ie., return $this->$cons;

Looks $this-> is having some problem. I use XAMPP version 1.7.2. Would there be any version issue?

oneworld
Use `$this->cons` instead of `$this->$cons` (see the missing `$`?). `$this->$cons` will look up the variable which name is saved in the `$cons` variable. That's not what you want ;)
nikic
A: 

Note that it is this:

$this->cons = $aCons;

and not this:

$this->$cons = $aCons;

If you specify $this->$cons, PHP will first look at the value in $cons and uses that as the name of the instance variable. In your case, there is nothing in $cons yet, so it will find a name that is empty, which causes the error message.

This type of indirection is not what you want to do here, so don't put the dollar sign in there twice!

Hollance
A: 

This is what I tried. Still the value comes as blank. The earlier $this=>$cons is my mistake. Sorry.

/* setter */

function setConstant($aCons) { $this->cons = $aCons; }

/* getter */

function getConstant() { $this->cons; }

oneworld
@user411873: you do need the `return` in the getter.
nikc
A: 

Hi Thanks for the answers. That was a very quick resolution. Appreciate it very much. Thanks everyone and all. Here is the complete code which works:

<?php
class MyClass
{
    var $cons;

    function showConstant() {
        $this->setConstant(100); /* assign value to variable here */
        $this->showConstantGetter();
    }

    /* setter */
    function setConstant($aCons) {
        $this->cons  = $aCons;
    }

    /* getter */
    function getConstant() {
        return $this->cons;
    }


    function showConstantGetter() {
        echo "<br>getting const : ".$this->getConstant(); /* use the variable's value in this method here */
    }

}

$classname = "MyClass";
$class = new MyClass();
$class->showConstant();

?>
oneworld