13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

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

166 Chapter 6 Object-Oriented <strong>PHP</strong><br />

How then do they work? If you instantiate the class<br />

$a = new classname();<br />

you can then use the __get() <strong>and</strong> __set() functions to check <strong>and</strong> set the value of any<br />

attributes.<br />

If you type<br />

$a->$attribute = 5;<br />

this statement implicitly calls the __set() function with the value of $name set to<br />

“attribute”, <strong>and</strong> the value of $value set to 5.You need to write the __set() function<br />

to do any error checking you want.<br />

The __get() function works in a similar way. If, in your code, you reference<br />

$a->attribute<br />

this expression implicitly calls the __get() function with the parameter $name set to<br />

“attribute”. It is up to you to write the __get() function to return the value.<br />

At first glance, this code might seem to add little or no value. In its present form, this<br />

is probably true, but the reason for providing accessor functions is simple:You then have<br />

only one section of code that accesses that particular attribute.<br />

With only a single access point, you can implement validity checks to make sure that<br />

only sensible data is being stored. If it occurs to you later that the value of $attribute<br />

should only be between 0 <strong>and</strong> 100, you can add a few lines of code once <strong>and</strong> check<br />

before allowing changes.You could change the __set() function to look as follows:<br />

function _set ($name, $value)<br />

{<br />

if( ($name="attribute") && ($value >= 0) && ($value attribute = $value;<br />

}<br />

With only a single access point, you are free to change the underlying implementation.<br />

If, for some reason, you choose to change the way $attribute is stored, accessor functions<br />

allow you to do this <strong>and</strong> change the code in only one place.<br />

You might decide that, instead of storing $attribute as a variable, you will retrieve it<br />

from a database only when needed, calculate an up-to-date value every time it is<br />

requested, infer a value from the values of other attributes, or encode the data as a smaller<br />

data type.Whatever change you decide to make, you can simply modify the accessor<br />

functions. Other sections of code will not be affected as long as you make the accessor<br />

functions still accept or return the data that other parts of the program expect.<br />

Controlling Access with private <strong>and</strong> public<br />

<strong>PHP</strong> uses access modifiers.They control the visibility of attributes <strong>and</strong> methods, <strong>and</strong> are<br />

placed in front of attribute <strong>and</strong> method declarations. <strong>PHP</strong> supports the following three<br />

different access modifiers:

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

Saved successfully!

Ooh no, something went wrong!