3

Possible Duplicate:
Workaround for basic syntax not being parsed

I don't understand how to use variables inside objects in php.

For example in javascript I would do this and it would be fine.

var foo = 10,
    bar = {option: foo+5}
;

console.log(bar.option);

in php I do the same but it fails

$variable = 10;

class myObject {
    var $username = $variable + 5 ;
    var $zzz = $username + 5;

}

Also is it possible to use $this in object or only in functions ?

And lastly how would I set up variable based on another inside the same object like in second line of myObject ? :)

Community
  • 1
  • 1
Tomas
  • 1,377
  • 3
  • 17
  • 32

4 Answers4

8

The right way would be to use a constructor - a method that will run when the object is created.

When you define class properties, you can only use constant values (1, 'xyz', array() are fine for example), not expressions that need to be evaluated at runtime.

Pass the variable to the constructor, and don't use global variables.

class myObject {
    public $username;
    public $zzz;

    public function __construct($variable) {
        $this->username = $variable + 5 ;
        $this->zzz = $this->username + 5;
    }
}

$obj = new myObject(10);
echo $obj->username; //15

In PHP, you don't need to use classes and objects if you don't want to write OOP code. You can simply create good old functions. But if you want to use them, try to get familiar with OOP concepts.

kapa
  • 77,694
  • 21
  • 158
  • 175
  • @Tomas A simple value. No function calls, operators, etc. – kapa Oct 19 '12 at 22:33
  • $obj = new myObject(10); why did you pass (10) ? – Tomas Oct 19 '12 at 22:42
  • @Tomas That is the magic 10 you wanted to use in your example (`$variable = 10`). Classes are something you don't have in Javascript, so you cannot follow the same thinking. – kapa Oct 19 '12 at 22:44
  • 1
    Thanks, I see now, and that 10 goes in the place of $variable here right public function __construct($variable) – Tomas Oct 19 '12 at 22:46
  • @Tomas Yes, it is the value passed to the constructor method. – kapa Oct 19 '12 at 22:46
5

Specifically, you asked how to use global variables in PHP. To do this you would use the global keyword at the time you define your variable:

$variable = 10;

class myObject {
    function foo () {
        global $variable;
        $username = $variable + 5 ;
        $zzz = $username + 5;
    }
}

BUT DON'T! It's a bad idea. It violates every principle of object encapsulation to do something like this, and kills a puppy every time your code runs. Ok, maybe the puppies are safe, but it's still not a good idea.

Instead you should pass the value to the object, either through the constructor, or through an accessor method. See below:

// The following code is guaranteed not to kill any puppies
class myObject {

    private $_variable;
    private $_userName;
    private $_zzz;

    public function __construct($value) {
        $this->_variable = $value;
        $this->_userName = $this->_variable + 5;
        $this->_zzz = $this->_userName + 5;
    }

}

$variable = 10;
$obj = new myObject($variable);
Joshua Kaiser
  • 1,461
  • 9
  • 17
  • 1
    In your first example `global` should have no effect (unless the file is included somewhere in a function). It is normally used INSIDE a function. And expressions cannot be used in the property declarations. – kapa Oct 19 '12 at 22:50
  • Thanks for pointing that out. I'll fix that. – Joshua Kaiser Oct 19 '12 at 22:52
  • Be sure to remove the `var`s as well - I guess `$this` needs to be used. – kapa Oct 19 '12 at 23:00
  • I took the vars out. You could use $this to make everything a property of the object, but the point I was trying to illustrate is that it CAN be done with a global, but really probably shouldn't. The second example is what I would gravitate toward. Thanks for your feedback! – Joshua Kaiser Oct 19 '12 at 23:03
  • Looks great now, let me upvote this. – kapa Oct 19 '12 at 23:04
0

Since you are access it variable outside the class i believe this is what you want to achieve

class myObject {
    public $username;
    public $zzz;
}

$variable = 10;
$obj = new myObject();
$obj->username = $variable + 5;
$obj->zzz = $obj->username + 5;

Or you can have

class MyObject {
    private $username;
    private $zzz;

    public function increment($variable) {
        $this->username = $variable + 5;
        $this->zzz = $this->username + 5;
    }
}

$variable = 10;
$obj = new MyObject();
$obj->increment($variable); // First Call
$obj->increment(5); // Canbe called multiple times 
Baba
  • 94,024
  • 28
  • 166
  • 217
0

You can have few types of variables in an object static and non-static but also public, private protected. I won't go in too many details but here's an example:

class MyClass 
{
   public $test = 'non-static';
   public static $otherTest = 'static';
   private $privateTest = 'private';
   const CONSTANT_TEST = 'constant';

   public function someMethod() {
       echo $this->$test; // non-static
       echo self::$otherTest; // static
       echo static::$otherTest; // static
       echo $this->privateTest; // private
   }

   public static staticMethod() {
       echo $this->test; // error
       echo self::$otherTest; // static
       echo static::$otherTest; // static
   }
}

$obj = new MyClass;
echo $obj->test; // non-static
echo $obj::otherTest; // static
echo MyClass::otherTest; // static
echo $obj->privateTest; // error
echo $obj::CONSTANT_TEST; // constant
echo MyClass::CONSTANT_TEST; // constant
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58