Read Php Easily

Page 90

OBJECT ORIENTATION IN PHP

PHP

VI

__set() The __set() magic function is used to complement the __get() function. This function is called when you set an undefined class variable in a program. Here we have given an example where we have used the ‘__set()’ magic function to develop a database table class. Assuming itself as the member of the class, it performs an unplanned enquiry. Example: <?php //...[snip - insert the MySQL connection code here]... class newtable { public $Naming; // public $AdministrativeEmail; public function __construct($Naming) { $this->Naming = $Naming; } public function __set($var, $val) { mysql_query(“UPDATE {$this->Naming} SET $var = ‘$val’;”); } // public $AdministrativeEmail = ‘foo@bar.com’; } $systemvars = new newtable(“systemvars”); $systemvars->AdministrativeEmail = ‘telrev@somesite.net’; ?> In the above example, $AdministrativeEmail is commented out. It is not available in the newtable class. $AdministrativeEmail is set on the last line and __set() is called here. It includes the name of the variable that is being set. __call() Another important magic function is the __call() function. Example: <?php class Bee { public $Naming; public function buzz() { www.thinkdigit.com

FAST TRACK

89


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.