Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,424 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,951 people online right now. Registration is fast and FREE... Join Now!




Advance function in php ...

 
Reply to this topicStart new topic

Advance function in php ...

myharshdesigner
18 Jan, 2008 - 10:59 PM
Post #1

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 24


My Contributions
CODE

function __autoload($class_name) {
include_once($class_name . "php");
}
$obj = new MyClass1();
$obj2 = new MyClass2();


can any one explane me this code by some example ?
i cant able to under stand how this code will work .

so pl help me to understand this code.

This post has been edited by myharshdesigner: 18 Jan, 2008 - 11:00 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Advance Function In Php ...
18 Jan, 2008 - 11:15 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
When you use objects in PHP or other object oriented languages, you have two parts. One is the class definition and the other is creating an instance of the class. The class definition is the blue print of how an object is built and the object call is creating an instance of that object.

Normally in PHP you include the class definition before you can use the class. After all, how is PHP suppose to know how to create a "Car" object without a class definition "Car" to show it how it works?

Many programmers put the class definition in a file and then just include it in a page whenever they need to use the class. Put the Car class definition in a file like "carclass.inc.php" and include it in a page where you need to build some car objects.

Autoloading allows you to load these include files dynamically whenever you attempt to create an instance of a class and no class definition is immediately found telling PHP how to build the class.

For example... lets say I include the autoload function in a file and then use something like... $mycar = new Car();. PHP attempts to find the definition of the Car class to show it how to build this car object. It can't find it because we never included it. So PHP turns to this autoload function and executes it. The autoload function is then responsible for handling the situation of you creating an object and PHP not knowing where to find the class file. Hence why the autoload function takes a class name as a parameter. In our instance the class name "Car" would be passed to the autoload function. We can use this name "Car" to then find the Car class include file by putting it in a require function or an include function.

In your example the first time we use "Myclass1" it can't find the definition and goes to autoload for help. Autoload fires with the word "Myclass1" which it uses to fetch the include file "Myclass1.php" and inserts it in the file, now PHP can find the class definition and it knows how to build the object. It "autoloaded" the class file for the definition.

Same with Myclass2... autoload gets called because it can't find the definition for the class, Myclass2.php is fetched and then included, now PHP knows how to build both classes and continues its work.

I talk more about this in my blog titled "Martyr2's Programming Underground - Autoloading in PHP 5".

Hopefully this explained a bit. smile.gif

This post has been edited by Martyr2: 18 Jan, 2008 - 11:17 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:36AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month