25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Object Oriented Programming in <strong>PHP</strong> ” 123<br />

tion—that’s something you can only do within one of the class’ methods (typically,<br />

the constructor).<br />

Constants, Static Methods and Properties<br />

Along with PPP, <strong>PHP</strong> 5 also implements static methods and properties. Unlike regular<br />

methods and properties, their static counterparts exist and are accessible as part<br />

of a class itself, as opposed to existing only within the scope of one of its instances.<br />

This allows you to treat classes as true containers of interrelated functions and data<br />

elements—which, in turn, is a very handy expedient to avoid naming conflicts.<br />

While <strong>PHP</strong> 4 allowed you to call any method of a class statically using the scope<br />

resolution operator :: (officially known as Paamayim Nekudotayim—Hebrew for<br />

“Double Colon”), <strong>PHP</strong> 5 introduces a stricter syntax that calls for the use of the static<br />

keyword to convey the use of properties and methods as such.<br />

<strong>PHP</strong> is very strict about the use of static properties; calling static properties using<br />

object notation (i.e. $obj->property) will result in both a “strict standards” message<br />

and a notice. This is not the case with static methods, however calling a non-static<br />

method statically will also emit a “strict standards” message.<br />

class foo {<br />

static $bar = "bat";<br />

public static function baz()<br />

{<br />

echo "Hello World";<br />

}<br />

}<br />

$foo = new foo();<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

$foo->baz();<br />

echo $foo->bar;<br />

This example will display:<br />

Hello World<strong>PHP</strong> Strict Standards: Accessing static property foo::$bar as non<br />

static in <strong>PHP</strong>Document1 on line 17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!