11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 • OBJECT-ORIENTED <strong>PHP</strong>www.it-ebooks.info// Instantiate another Visitor class.$visits2 = new Visitor();echo Visitor::getVisitors()."";?>The results are as follows:12Because the $visitors property was declared as static, any changes made to its value (in this casevia the class constructor) are reflected across all instantiated objects. Also note that static properties andmethods are referred to using the self keyword and class name, rather than via $this and arrowoperators. This is because referring to static properties using the means allowed for their “regular”siblings is not possible and will result in a syntax error if attempted.■ Note You can’t use $this within a class to refer to a property declared as static.The instanceof KeywordThe instanceof keyword was introduced with <strong>PHP</strong> 5. With it you can determine whether an object is aninstance of a class, is a subclass of a class, or implements a particular interface, and do somethingaccordingly. For example, suppose you want to learn whether $manager is derived from the classEmployee:$manager = new Employee();...if ($manager instanceof Employee) echo "Yes";Note that the class name is not surrounded by any sort of delimiters (quotes). Including them willresult in a syntax error. The instanceof keyword is particularly useful when you’re working with anumber of objects simultaneously. For example, you might be repeatedly calling a particular functionbut want to tweak that function’s behavior in accordance with a given type of object. You might use acase statement and the instanceof keyword to manage behavior in this fashion.Helper FunctionsA number of functions are available to help the developer manage and use class libraries. Thesefunctions are introduced in this section.154

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

Saved successfully!

Ooh no, something went wrong!