Classes and :: operator

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Classes and :: operator

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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);
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
Post Reply