HOME       >>       Programming

Php Magic Method Magic:To represent the thinks


veerumits

PHP 5, have magic method:--

The __autoload() Magic Method:- The __autoload() magic method get automatically called whenever you try to load an object of the class in which resides in separate file and even you have not included those files using any of these method include,require and include_once. but It is neccessary to use the filename as that of the class name.

for example:-
//file name: abc.php;

<?phpclass abc{var $user="Raju";var $city="delhi";function xyz(){ echo "Hello ".$this->user." Your city is ".$this->city; }}?>
//file name: xyz.php;
<?phpfunction __Autoload($x){ include_once("$x.php"); }$obj=new abc;echo $obj->user."<br>";echo "   <br>".$obj->xyz();?>

output when you brows xyz.php:

Raju
Hello Raju Your city is delhi

here what we see in xyz.php not have any class with the name of abc even i have create here object of class abc,
it is automatically called because i am using magic method __autoload($x), $x is variable which is assign by the name of filename abc and in include _once("$x.php"); it become include _once("abc.php"); so this is magic because it represent something like magic.

both file should be in same directory.

thanks.


VIEW ORIGINAL REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.