PHP coding standard: using self::method() vs $this->method()

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
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

PHP coding standard: using self::method() vs $this->method()

Post by jeff00seattle »

If a class method() is not static, and even though it works calling it with self::method(), I want to know if this is totally uncool and does not meet to PHP coding standards?

To meet to PHP coding standards (PEAR), then must I restrict myself to:
  • Use only $this-> when referring to non-static class methods and member variables
  • Use only self:: when referring to static methods and constants
Advise?

Thanks

Jeff in Seattle
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP coding standard: using self::method() vs $this->method()

Post by josh »

I think it will throw an E_STRICT. Its not "cool" to use static methods unless there is no "state"

$result = Math:add( 5, 5 ); // legit place to use "static", there is no state
$cart->changeQuantity(5); // if there is "state", a conceptual "object", static usage leads to "global state"

http://www.youtube.com/watch?v=-FRm3VPhseI
Post Reply