Page 1 of 1

Classes and :: operator

Posted: Mon Nov 27, 2006 7:32 pm
by Mr Tech
I'm trying to get my head around this tutorial: http://www.phpfreaks.com/phpmanual/page ... tayim.html

Does it mean you can only use A::example(); if the A class has no var's?

Or does it mean you can only use A::example(); if the example function class is function example() instead of function example($a, $b)?

If someone can explain it, it would be greatly appreciated. I can't make any sense of what they've writeen.

Posted: Mon Nov 27, 2006 7:40 pm
by Burrito
all the :: means is you're calling the method statically.

ex:

Code: Select all

class Foo
{
  public function __construct()
  {
     // nothing
  }
  public function addStuff($num)
  {
     return $num + 4;
  }
  
}

// 4 + 4 = 8
echo Foo::addStuff(4);

Posted: Mon Nov 27, 2006 8:09 pm
by Mr Tech
So instead of this:

Code: Select all

$foo = new Foo();
echo $foo->addStuff(4);
You would use your example above?

Lastly, I've seen people add an & character before the new:

Code: Select all

$foo = &new Foo();
echo $foo->addStuff(4);
What does that mean?

Thanks again!

Posted: Mon Nov 27, 2006 8:34 pm
by Burrito
Mr Tech wrote:So instead of this:
...
You would use ...
if you wanted to call the method statically, yes. You're not creating an object by doing it this way.
Mr Tech wrote: Lastly, I've seen people add an & character before the new:
...
What does that mean?
you're creating a variable reference.

Posted: Tue Nov 28, 2006 3:19 am
by Maugrim_The_Reaper
The tutorial was written for PHP4, so bear that in mind. Many of the traditional uses of the & prefix (creating a reference) are not required in PHP5 whose default behaviour is to pass by reference.

You can access a method statically, which does not require you to instantiate an object using the "new" keyword. The issue is that a static method cannot alter/access the properties of the object it's attached to unless they too are static. In PHP5, static methods or variables are typically set as static using the PHP5 keywords:

Code: Select all

class MyObject {

    public static $static_property = 0;

    public static function myStaticMethod()
    {
        self::$static_property++;
    }

}
You should also check into the "self" and "parent" references in the manual.

Posted: Tue Nov 28, 2006 4:05 am
by Chris Corbyn
I'll take a shot at the simple explanation.

Static members of a class are pretty much monostate. In plain English, that means that no matter how many times you instantiate the class, changes to the static members will persist in each instance. However, because the static members play no part in the "object" itself, they cannot access anything contained within $this since $this relates to an object. They're basically nothing more than functions stuck inside a class.

Posted: Tue Nov 28, 2006 5:21 am
by Mordred
Actually PHP4 (dunno about 5) behaves strangely when calling statically functions that use $this:

Code: Select all

class A {
	var $member = 0;
	function F() {
		return $this->member;
	}
};
class B {
	var $member = 1;
	function Pr() {
		echo A::F(); //static call you say?
	}
};
$b = new B();
$b->Pr();
This outputs 1 - the $this is not bound to the class the method is defined in, but rather to the object.
This is also apparent in inheritence, when you can call CParent::CParent(), which still uses $this.

Posted: Tue Nov 28, 2006 5:56 am
by Maugrim_The_Reaper
This is the PHP4 OOP behaviour. The static call on a public function works a bit like a standalone function, where external $this references are checked against the current variable scope. The result is the static call to A, resolves $this to the current class where it is in scope, and the object by virtue of being instantiated makes it accessible.

It's behaviour like this messes up OOP...

In PHP5, this behaviour is retained (PHP4 BC presumably). However the PHP5 OOP model (when used as intended) differs. In PHP5 a method aimed at being statically called (i.e. public static, not simply public) would produce a fatal error with the same usage:

Code: Select all

class A {
    private $member = 0;
    public static function F() {
        return $this->member;
    }
};
class B {
    private $member = 1;
    public function Pr() {
        echo A::F(); //static call you say?
    }
};
$b = new B();
$b->Pr();
Fatal error: Using $this when not in object context in /opt/server/htdocs/test.php on line 6
Removing the static keyword above still plants you up against the fact $member is private. Shows A attempting to access the private $member property of B on the static call.