Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5537

Autoload your classes

$
0
0

While writing object oriented applications we creates class files that we include at the top of our page before creating objects of them.We could have have tens of class  files for even a single program and keep track of them and include them is little tedious .In order to eliminate this we could use “__autoload” function.This is function is automatically called in case you are trying to use a class that hasn’t been defined/included yet.

This is how it works.

Suppose we have two  class files  Test and Images.

<?php

  function __autoload($class_name) {
      require_once $class_name . '.php';
  }
  
  $a = new Test();
  $b = new Image();
?>

So in the above case you do need to include the respective files as autoload function will take care of it when you will create objects of them.

 

 

Facebook Twitter Email Reddit Stumbleupon

Viewing all articles
Browse latest Browse all 5537