Page 1 of 1

Static Methods

Posted: Wed Jul 19, 2006 12:15 pm
by Son Volt
I'm reading the code below from this website and came accross this explanation and example code
Essentially, all methods can be called statically in PHP5 anyway, as was possible in PHP4. However, the static keyword prevents a method declared with it from being called via an object instance, as the example demonstrates.
http://www.sitepoint.com/print/1192

It says that the method that was declared static cannot be called from an object instance. Well I'm testing this code and sure enough it DOES let me call static Foo() from an object instance. I get no error. Is the example just wrong or is there a setting in the php.ini that enforces this?

Code: Select all

<?php
class MyStatic {
   static function Foo() {
       return 'This is foo()';
   }
   function Bar() {
       return 'This is bar()';
   }
}

echo ( MyStatic::foo().'<br />' );
echo ( MyStatic::bar().'<br />' );

$obj = new MyStatic();

# Fatal error - cannot call static method via object instance
// echo ( $obj->foo().'<br />' );
echo ( $obj->bar().'<br />' );
?>
Script: static_method.php

Posted: Wed Jul 19, 2006 12:17 pm
by Ollie Saunders
put this above your code:

Code: Select all

error_reporting(E_ALL | E_STRICT);
and you will get an error :)

Posted: Wed Jul 19, 2006 12:26 pm
by Son Volt
ole wrote:put this above your code:

Code: Select all

error_reporting(E_ALL | E_STRICT);
and you will get an error :)

Hey that worked thanks!... Does E_STRICT encompass E_NOTICE as well or do I need to add that additionally?

Posted: Wed Jul 19, 2006 12:32 pm
by Ollie Saunders
no E_ALL encompasses everything but E_STRICT which you have to add in yourself.

Posted: Wed Jul 19, 2006 12:39 pm
by Son Volt
Oh wait... I AM able to call the static method from an object instance. The error that is generated is when I call MyStatic::bar()... it says I shouldn't call it statically since it's not declared static. But in my original question where the article says "calling a static method from an object instance will generate an error" such as $obj->foo() is actually not causing an error. How do I prevent an object instance from calling a static method as the article suggests? Unless I'm reading it wrong...
However, the static keyword prevents a method declared with it from being called via an object instance, as the example demonstrates.

Posted: Wed Jul 19, 2006 1:20 pm
by Ollie Saunders
Ah. You can't sorry :(
Yeah, I know, it sucks.

Posted: Wed Jul 19, 2006 1:22 pm
by Ollie Saunders
Just thought you can do this:

Code: Select all

static public function someStatic()
{
    if (isset($this)) trigger_error('Not called statically '. __FUNCTION__);
    // whatever
}
But I wouldn't advise it, because its extra work and slows performance, just try to be aware of how you should be calling stuff.

Posted: Wed Jul 19, 2006 1:33 pm
by Christopher
I just want to clarify because it is easy to get confused on this. The thing about statics is that they are declared so that they can only be called statically. However, that does not limit their use -- it only the syntax used to call them.

So from your example:

Code: Select all

class MyStatic {
   static function foo() {
       $this->bar();        // NOT ok
       return 'This is foo()';
   }
   function bar() {
       MyStatic::foo();    // ok
       self::foo();             // ok
       return 'This is bar()';
   }
}

// These are all ok
MyStatic::foo();
$obj = new MyStatic();
$obj->bar();