Jump to content

PHP Ignoring Static Keyword?


NightCabbage

Recommended Posts

Hey everyone

 

Just got an odd problem here...

 

I've set up a class, as the example below, and it seems to be ignoring the use of the "static" keyword completely.

 

<?php
class Happy
{
    static function printBlah()
    {
        echo "Blah";
    }
    
    function printMoo()
    {
        echo "Moo";
    }
}

Happy::printBlah();
Happy::printMoo();

$pie = new Happy();
$pie->printBlah();
$pie->printMoo();
?>

 

Output:

 

BlahMooBlahMoo

 

I'd expect the output to be something like:

 

Blah[error][error]Moo

 

Any idea what's going on here?

 

I'm probably just doing something stupid (am quite tired)...

 

Thanks!

Link to comment
Share on other sites

From http://us3.php.net/manual/en/language.oop5.static.php

 

"A member declared as static can not be accessed with an instantiated class object (though a static method can)."

 

A static method is very different from a static member.  A method is still the same method whether it is called statically or from an instance.  Members are different because they require storage space, and they must be stored either globally (static) or per-instance (non-static).

Link to comment
Share on other sites

A static method is very different from a static member. A method is still the same method whether it is called statically or from an instance. Members are different because they require storage space, and they must be stored either globally (static) or per-instance (non-static).

Thanks btherl :)

 

I'm from a programming background, so I didn't expect this behaviour!

 

So there's no difference between a static and a non-static function?

(other than for readability and human reference?)

 

(how do you mark a topic as solved?)

Link to comment
Share on other sites

Yep, PHP treats it as an advisory thing, not as a declaration of how you will use it.  PHP is a bit loose in its enforcement of things like this.  It enforces things when it is forced to (because it affects implementation), but lets a lot of other things go.

 

Mark, you can call any method in php without instantiating the class, but php will generate a warning if it's not declared static.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.