Friday, October 4, 2013

How to auto load file class in PHP

Here's the sample how to load class in the file of PHP
here we go! Let's put two files in the html(/var/www/html) directory on your computer.
First file contain of Class code:
# sample.php

< ?php

class Sample
{
    // property declaration
    public $var = 'hello world';

    // method declaration
    public function display() {
        echo $this->var;
    }
}

? >

Second file:
# load.php

< ?php

function __autoload($class_name) {
    include strtolower($class_name) . '.php';
}

$obj  = new Sample();

echo $obj->display();

? >

Next step is access load.php file from localhost:
http://localhost/load.php for more visit this page

No comments:

Post a Comment