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.infoMario__get called!No such variable!Creating Custom Getters and SettersFrankly, although there are some benefits to the __set() and __get() methods, they really aren’tsufficient for managing properties in a complex object-oriented application. Because <strong>PHP</strong> doesn’t offersupport for the creation of properties in the fashion that Java or C# does, you need to implement yourown solution. Consider creating two methods for each private property, like so:Although such a strategy doesn’t offer the same convenience as using properties, it doesencapsulate management and retrieval tasks using a standardized naming convention. Of course, youshould add additional validation functionality to the setter; however, this simple example should sufficeto drive the point home.ConstantsYou can define constants, or values that are not intended to change, within a class. These values willremain unchanged throughout the lifetime of any object instantiated from that class. Class constants arecreated like so:const NAME = 'VALUE';For example, suppose you create a math-related class that contains a number of methods definingmathematical functions, in addition to numerous constants:class mathFunctions{const PI = '3.14159265';const E = '2.7182818284';const EULER = '0.5772156649';144

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

Saved successfully!

Ooh no, something went wrong!