Object Overloading with __get(), __post() and __call()
PHP lets you use a technique known as overloading to intercept attempts to read or write an object ’ s properties, or call its methods. This can be quite powerful. As far as the calling code is concerned, the object contains fixed, pre - programmed properties and methods. However, behind the scenes, your object can be doing all sorts of interesting things. For example:
The calling code reads the value of $Object - > property, which actually causes $Object to retrieve the value from an array instead.
The calling code sets $Object - > anotherProperty to a new value, but behind the scenes $Object actually writes this value to a database field.
The calling code calls $Object->Method(). This method doesn’t actually exist in $Object, but $Object intercepts the call and calls another method instead.
PHP allows you to create three “magic” methods that you can use to intercept property and method accesses:
• __get () is called whenever the calling code attempts to read an invisible property of the object.
• __set () is called whenever the calling code attempts to write to an invisible property of the object.
• __call () is called whenever the calling code attempts to call an invisible method of the object.
What is meant by “invisible”? In this context, invisible means that the property or method isn’t visible to the calling code. Usually this means that the property or method simply doesn’t exist in the class, but it can also mean that the property or method is either private or protected, and hence isn’t accessible to code outside the class.
• By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them.
• Ideally in a strict language this would have been an error. But, with PHP this works perfectly well as you can assign values to an undefined variable.
• Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.
• __set() allows you to provide functionality to validate data being stored. See example below:
• In the above example when Harshida@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. Harshida@domain.com is passed to $vl variable of the __set() method.
• The next step is to store these values into the $data array so that you could retrieve it later.
• The __get() method works in the similar fashion. When you echo $c->email, __get() method is called and the name email is passed in the $dt of the __get() method.
It is possible to stop this behavior of PHP to assign values to undefined issues. The solution is that you raise an exception from within __set() method.
Overloading Method Calls with __call
• __call() is to undeclared methods what __get() and __set() are to undeclared data member.
• __call() takes two arguments. The first argument is the name of the undeclared method invoked by the program and the second is an array that contains a list of parameters passed to the undeclared array.
• Using this method, you can provide code to handle calls to undeclared method. To disallow programs to call an undeclared method, you should raise an exception from within __call() magic method.
PHP lets you use a technique known as overloading to intercept attempts to read or write an object ’ s properties, or call its methods. This can be quite powerful. As far as the calling code is concerned, the object contains fixed, pre - programmed properties and methods. However, behind the scenes, your object can be doing all sorts of interesting things. For example:
The calling code reads the value of $Object - > property, which actually causes $Object to retrieve the value from an array instead.
The calling code sets $Object - > anotherProperty to a new value, but behind the scenes $Object actually writes this value to a database field.
The calling code calls $Object->Method(). This method doesn’t actually exist in $Object, but $Object intercepts the call and calls another method instead.
PHP allows you to create three “magic” methods that you can use to intercept property and method accesses:
• __get () is called whenever the calling code attempts to read an invisible property of the object.
• __set () is called whenever the calling code attempts to write to an invisible property of the object.
• __call () is called whenever the calling code attempts to call an invisible method of the object.
What is meant by “invisible”? In this context, invisible means that the property or method isn’t visible to the calling code. Usually this means that the property or method simply doesn’t exist in the class, but it can also mean that the property or method is either private or protected, and hence isn’t accessible to code outside the class.
• By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them.
• Ideally in a strict language this would have been an error. But, with PHP this works perfectly well as you can assign values to an undefined variable.
• Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.
• __set() allows you to provide functionality to validate data being stored. See example below:
• In the above example when Harshida@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. Harshida@domain.com is passed to $vl variable of the __set() method.
• The next step is to store these values into the $data array so that you could retrieve it later.
• The __get() method works in the similar fashion. When you echo $c->email, __get() method is called and the name email is passed in the $dt of the __get() method.
It is possible to stop this behavior of PHP to assign values to undefined issues. The solution is that you raise an exception from within __set() method.
Overloading Method Calls with __call
• __call() is to undeclared methods what __get() and __set() are to undeclared data member.
• __call() takes two arguments. The first argument is the name of the undeclared method invoked by the program and the second is an array that contains a list of parameters passed to the undeclared array.
• Using this method, you can provide code to handle calls to undeclared method. To disallow programs to call an undeclared method, you should raise an exception from within __call() magic method.




No comments:
Post a Comment