Requiring classes in a Zend Framework based library

Simple question. Why use require_once

to include the classes you use in the file? For example, if I create a class that extends Zend_Db_Table_Abstract

, I really don't need to have this in the class declaration file:

require_once "Zend/Db/Table/Select.php";

      

They are included in the autoloader anyway. Is there a performance gain for explicitly declaring "imports"? Is this for unit testing?

+2


source to share


2 answers


Simple answer: if you don't use an autoloader, the functionality won't break.

You don't need to do this in your project if you know you will be using an autoloader, but if you are working on a framework (like Zend) that will be used by other developers as well, you will need to be able to be reusable with a minimum of compatibility issues so you really need explicit dependencies.

Another benefit is that it helps you see when your classes become dependent on too many other classes. This will help you keep coupling to a minimum when designing your facilities.



Example from Zend/Db/Table/Select.php

/**
 * @see Zend_Db_Select
 */
require_once 'Zend/Db/Select.php';


/**
 * @see Zend_Db_Table_Abstract
 */
require_once 'Zend/Db/Table/Abstract.php';

      

+6


source


You can get rid of these calls in your IDE by replacing "require_once" with "// require_once" require_once should only be used to load classes. Others like inserting an array from a file like $file = include "test.php";

, don't use it (or don't need to use it).



0


source







All Articles