1

Hi I'm a bit of a newbie to OOP, i just have a quick question: say I have a function in a class declared as

class House
{
    public static function hasAlcohol() 
    {
        // Do Something
    }
}

I know i can call this as

House::hasAlcohol()

However, i would also like to know if its okay with coding standards and PHP and if it would be error free to call hasAlcohol() from an instance of house (i tried it and got no errors), for example

$house = new House();
$house->hasAlcohol();
4
  • 1
    That's a sign of poor design. If the function is static, you shouldn't want to do that. Commented Jan 5, 2012 at 22:51
  • Check stackoverflow.com/questions/2095529/…, that looks like the same question. Commented Jan 5, 2012 at 22:51
  • you can always create one-shot script to test it. you know, typing and executing it yourself is a better way to learn programming Commented Jan 5, 2012 at 22:55
  • hey silent, i tried it and it works, but just wanted to know if its good coding practice, which everyone says is not. That was the whole idea of posting here. Thank you Commented Jan 5, 2012 at 22:57

3 Answers 3

5

As this has caused several problems for me in the past: Yes, it is valid code. Should you do it? No. It gives the impression that the call is non-static and will most likely cause grief for people working on your code later on. There is no reason to make your code ambiguous.

This used to be possible, but the latest versions of PHP will throw an error, if I remember correctly. You should call static functions statically. You can do $house::hasAlcohol() though.

Sign up to request clarification or add additional context in comments.

Comments

1

This used to be possible, but the latest versions of PHP will throw an error, if I remember correctly. You should call static functions statically. You can do $house::hasAlcohol() though.

On a side note, should hasAlcohol really be static? From the name it appears it should be an instance method.

3 Comments

thank you very much. i just used a hypothetical function hasAlcohol(), lol.
clearly every house has alcohol therefore: public static function hasAlcohol() { return true; }
@Andrew True, I guess the real logic should be in public function shouldHaveAlcohol() { return '*cough*depends*cough*'; } then.
1

A more recommended pattern if you need constant access to a method is to use a static constructor and get an instance (even if it's a "blank" or "empty") instance to that class. So in the example you've shown, it might be better to have a method like this:

class House
{
    public function instance()
    {
        return new House; 
    }

    public function hasAlcohol() 
    {
        // Do Something
    }
}

Then if you ever needed to make a call to "hasAlcohol()" where you don't need an instance for any other purpose, you can do a one-off like so:

House::instance()->hasAlcohol();

or you can instantiate it like in your example:

$house = new House;
$house->hasAlcohol();

or, better yet, use your new factory method:

$house = House::instance();
$house->hasAlcohol();

3 Comments

Sorry, I don't see how this is any better or different than House::hasAlcohol(). If the method makes sense as a static method, make it static and call it statically. If it requires an instance, it doesn't make sense to call it as a "one off". A factory whose only job is to call the new operator seems pretty superfluous, especially if your only reason to include it is to be able to call a "static" method as instance method.
Sorry, I was making a perhaps incorrect assumption that the method needed to function as an instance method. The static version wouldn't be able to access instance properties. In the given example, I assume if the class can/should be instantiated (e.g., you may have more than a single house, and that house may-or-may not have alcohol). I realize I may have been answering a question other than the one that was specifically asked.
Hey guys, thanks, the all the answers are very educational to me, thank you. I have learnt a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.