Static Methods

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Static Methods

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

put this above your code:

Code: Select all

error_reporting(E_ALL | E_STRICT);
and you will get an error :)
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

no E_ALL encompasses everything but E_STRICT which you have to add in yourself.
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ah. You can't sorry :(
Yeah, I know, it sucks.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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();
(#10850)
Post Reply