This is an outdated version of PHPasap. Please visit http://phpasap.com/docs for the latest version.

Libraries (Custom Classes)

In MVC architecture HTTP requests are handled by a Controller. Now every time writing all your business logic in one Controller method can make the code difficult to understand. For certain part of your code that you use very often, like say sending mails, you can create your own class that does this common job. Now instead of writing the same code again and again, your Controller can simply call this class thus following the principles of DRY (DONOT REPEAT YOURSELF).

Classes in PHPasap

You can place your custom class file anywhere in project root, like say in app directory or in app/folder1/folder2. You can autoload your class in the Controller as long as you are following the below namespace and file naming convention.

  • Each class will have it's own php file.
  • The name of the file will be same as the lower case'd name of the class. Eg: Class Some_Class_Name will be in some_class_name.php
  • Every class should be namespaced as per its parent directory structure. So a class Some_Class_Name located in app/folder1/folder2 should look like below:

    app/folder1/folder2/some_class_name.php
    namespace app\folder1\folder2;
    class Some_Class_Name {
        
    }
    

    To call the above file in your Controller you would reference it by its fully qualified name. Consider example below

    app/controllers/some_controller.php
    namespace app\controllers;
    class Some_Controller {
        public function index() {
            
            //note that Some_Class_Name is referenced by its fully qualified name i.e with namespace included
            $some_class_obj = new \app\folder1\folder2\Some_Class_Name();
            
        }
    }
    
  • You don't need to require or include the file, as long as you follow above convention, since it will be autoloaded by PHPasap.

Libraries

The term library must be very much familiar if you have worked on frameworks like Codeigniter. In traditional frameworks whenever you want to add your own class you can do so by creating a library. Library is nothing but a php file just as above. The difference is in the way the frameworks loads this library. Like in case of codeigniter you would do something like $this->load->library('library-file-name'). As discussed in above section, in PHPasap you can place your class file anywhere. But still for providing a starting point and for making a veteran MVC developer feel at home we provide a folder libraries in app directory. Going by the naming convention discussed in earlier section below is how you would add a library

app/libraries/library_example.php
namespace app\libraries;
class Library_Example {
    
}

To access this library in your Controller

app/controllers/some_controller.php
namespace app\controllers;
class Some_Controller {
    public function index() {
        
        //note that Library_Example is referenced by its fully qualified name i.e with namespace included
        $library_example_obj = new \app\libraries\Library_Example();
        
    }
}