HoHalla

In PHP, if we need to use the functions of class file, to load the class file, we usually use include/include_once or require/require_once functions. But the easier way is to use the inbuild function __autoload().

For demonstration define the class files as needed. Example is given below.



class MyClass1{

function myfunction(){
echo "Function inside MyClass1";
}
}
Save the file as MyClass.php


class MyClass2{

function mynextfunction(){
echo "Function inside MyClass2";
}
}
Save the file as MyClass2.php


Now instead of including the file use the function defination like below in the page where needed.


function __autoload($classname){
require_once($classname.".php");
}


Now instantiate the object of the class and call the function as below.

$obj1=new MyClass1();
$obj2=new MyClass2();

$obj1->myfunction();
$obj2->mynextfunction();

Share 

Add a Comment

You need to be a member of HoHalla to add comments!

Join this Ning Network

About

Translate This Network

Events

Music

Loading…

© 2009   Created by Shakeel Shrestha on Ning.   Create a Ning Network!

Badges  |  Report an Issue  |  Privacy  |  Terms of Service

Sign in to chat!