Jump to content
xisto Community
Sign in to follow this  
veerumits

Php Magic Method Magic:To represent the thinks

Recommended Posts

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.
Edited by veerumits (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.