Implementing static methods


show more Implementing static methods provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP show less
please wait ...

Implementing static methods

Static methods are similar to regular methods in a number of ways. The naming convention is the same, they can have a visibility scope, and static methods can return values. However, static methods cannot use the pseudo variable $this to get properties, as it refers to the current object. If you want to try anyway, you can, but you will get a fatal error. Instead, use the self keyword, which refers to the current class. This sounds a bit abstract, so I'll demonstrate a static method by implementing an address type id validator.

I'm going to open the Address class, and then start declaring a static method. Like static properties, I'll need to include the static keyword. Static public function isValidAddressTypeId with one argument ($address_type_id). Add the documentation, Determine if an address type is valid. We'll take an integer and return boolean.

To validate, check to see if the address _type_id is in the array keys of valid address types. return array_key_exists. The ke...

Implementing static methods
Video duration: 5m 43s 1h 57m Intermediate Updated Apr 10, 2013

Viewers:

Implementing static methods provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP

Subject:
Developer
Software:
PHP
Author:
please wait ...