views:

57

answers:

3

I was wondering if there is a shorter, better or cleaner way to assign and use class variables in PHP, then through $this->instance_variable ?

class Bar {
  #  internal variables
  var $foo = "Hello World";

  public function foo() {
    return $this->foo;
  }
}

I am not very familiar with all PHP's variable scoping, but from what I understood at the official docs, you can either define them global, or access them trough $this->? Is there a keyword to define them as instance variable, so they are accessible like Rubys @variable?

+1  A: 

If you have an instance variable (like your $foo) the only way to access it is through $this->foo.

You could, if desperate, write something like $foo =& $this->foo; so you have a local reference to the same variable, but the only benefit is saving a few characters at the cost of added confusion.

VoteyDisciple
+2  A: 

PHP instance properties are accessed via the $this->variable operator internally, and public properties as $object->property.

You can access a class variable internally via self::$variable, which is similar to Ruby's class variables. Discussion of self:: vs. $this-> in PHP

In general, you will find PHP much more verbose than Ruby, not least in terms of characters required to reference a variable.

Clay Hinson
+4  A: 

There's a few key assumptions your'e bringing from ruby that don't translate well into PHP.

The correct way to declare and use object properties (equivalent to Ruby's instance variables) in PHP is

class Foo
{
    //accesible from inside this objects of this class, 
    //objects that have this class as an ancestor, and from
    //outside the object  
    //var $bar; is equivalent.  "var" is PHP 4 syntax, 
    //when everything was public
    public $bar;

    //accesible from inside this objects of this class, 
    //objects that have this class as an ancestor  
    protected $baz;

    //accesible from inside this objects only
    private $fiz;

    protected function example()
    {
        echo $this->bar . "\n";
        echo $this->baz . "\n";
        echo $this->fiz . "\n";
    }
}

PHP's OO syntax is based on the Java/C# view of the world. However, because every PHP page/script/program starts off in the global score, the $this pseudo reference to the local object is needed. Without it, you'd create a large degree of ambiguity around situations like this

//In main.php
$foo = "bar";
include('example.php');

//in example.php
class Example
{
    public $foo="baz";
    public function scopeIsHardLetsGoShopping()
    {
        global $foo;
        echo $foo;
    }
}

So, in the method should the $foo referenced be the object variable, or the global variable? If you say it should be the object variable, how do you access the global foo from a method? If you say it should be the global variable how do you access the local property after declaring a variable with the same name global?

Ruby and python gave scoping some thoughts at the onset of the language, so these problems can be avoided. PHP started as a quick way to hack in some c-code to process forms and output HTML. Because PHP makes reasonable efforts to be backwards compatible, you end up with quirky work arounds like $this.

Coming form Ruby it seems a little verbose, but it's a fundamental part of PHP.

Alan Storm