| | 105 | |
|---|
| | 106 | Class autoloading |
|---|
| | 107 | ----------------- |
|---|
| | 108 | |
|---|
| | 109 | The autoloading feature allows you to create an new object without requiring the file that contains its class definition by hand: |
|---|
| | 110 | |
|---|
| | 111 | [php] |
|---|
| | 112 | $obj = new MyClass(); |
|---|
| | 113 | // If MyClass is autoloaded, this will work without a require |
|---|
| | 114 | |
|---|
| | 115 | The autoloading is configured to work with the `lib/` directories described above, according to the default `autoload.yml` configuration file (found in `$pear_data_dir/symfony/config/autoload.yml`): |
|---|
| | 116 | |
|---|
| | 117 | autoload: |
|---|
| | 118 | |
|---|
| | 119 | symfony_core: |
|---|
| | 120 | name: symfony core classes |
|---|
| | 121 | ext: .class.php |
|---|
| | 122 | path: %SF_SYMFONY_LIB_DIR% |
|---|
| | 123 | recursive: on |
|---|
| | 124 | exclude: [vendor] |
|---|
| | 125 | |
|---|
| | 126 | symfony_orm: |
|---|
| | 127 | name: symfony orm classes |
|---|
| | 128 | files: |
|---|
| | 129 | Propel: %SF_SYMFONY_LIB_DIR%/addon/propel/sfPropelAutoload.php |
|---|
| | 130 | Criteria: %SF_SYMFONY_LIB_DIR%/vendor/propel/util/Criteria.php |
|---|
| | 131 | SQLException: %SF_SYMFONY_LIB_DIR%/vendor/creole/SQLException.php |
|---|
| | 132 | DatabaseMap: %SF_SYMFONY_LIB_DIR%/vendor/propel/map/DatabaseMap.php |
|---|
| | 133 | |
|---|
| | 134 | symfony_plugins: |
|---|
| | 135 | name: symfony plugins |
|---|
| | 136 | ext: .class.php |
|---|
| | 137 | path: %SF_PLUGIN_DIR% |
|---|
| | 138 | recursive: on |
|---|
| | 139 | |
|---|
| | 140 | project: |
|---|
| | 141 | name: project classes |
|---|
| | 142 | ext: .class.php |
|---|
| | 143 | path: %SF_LIB_DIR% |
|---|
| | 144 | recursive: on |
|---|
| | 145 | exclude: [model, plugins, symfony] |
|---|
| | 146 | |
|---|
| | 147 | model: |
|---|
| | 148 | name: project model classes |
|---|
| | 149 | ext: .php |
|---|
| | 150 | path: %SF_MODEL_LIB_DIR% |
|---|
| | 151 | |
|---|
| | 152 | application: |
|---|
| | 153 | name: application classes |
|---|
| | 154 | ext: .class.php |
|---|
| | 155 | path: %SF_APP_LIB_DIR% |
|---|
| | 156 | |
|---|
| | 157 | If you want to add other places for symfony to look into, create your own `autoload.yml` file in an application `config/` directory and add in new rules. Each autoloading rule has a label, both for visual organization and ability to override it. |
|---|
| | 158 | |
|---|
| | 159 | For instance, if you added your own `MyClass` class in `/usr/local/mycustomclasses/`, you could add: |
|---|
| | 160 | |
|---|
| | 161 | mycustomclasses: |
|---|
| | 162 | name: classes that I developed myself |
|---|
| | 163 | ext: .php |
|---|
| | 164 | path: /usr/local/mycustomclasses/ |
|---|
| | 165 | recursive: on |
|---|
| | 166 | |
|---|
| | 167 | Third-party libraries |
|---|
| | 168 | --------------------- |
|---|
| | 169 | |
|---|
| | 170 | If you need a functionality provided by a third-party class, and if you don't want to copy this class in one of the symfony `lib/` dirs, you will probably install it outside of the usual places where symfony looks for files. In that case, using this class will imply a manual `require` in your code, unless you use the symfony **bridge** to take advantage of the autoloading. |
|---|
| | 171 | |
|---|
| | 172 | Symfony doesn't (yet) provide tools for everything. If you need a PDF generator, an API to Google Maps or a PHP implementation of the Lucene search engine, you will probably need a few libraries from the [Zend Framework](http://framework.zend.com/). If you want to manipulate images directly in PHP, connect to a POP3 account to read emails, or design a console interface for Linux, you will choose the libraries from [eZcomponents](http://ez.no/products/ez_components). |
|---|
| | 173 | |
|---|
| | 174 | Fortunately, if you define the right settings, the components from both these libraries will work out of the box in symfony. |
|---|
| | 175 | |
|---|
| | 176 | The first think that you need to declare (unless you installed the third party libraries via PEAR) is the path to the root directory of the libraries. This is to be done in the application `settings.yml`: |
|---|
| | 177 | |
|---|
| | 178 | .settings: |
|---|
| | 179 | zend_lib_dir: /usr/local/zend/library/ |
|---|
| | 180 | ez_lib_dir: /usr/local/ezcomponents/ |
|---|
| | 181 | |
|---|
| | 182 | Then, extend the autoload routine by telling which library to look at when the autoloading fails with symfony: |
|---|
| | 183 | |
|---|
| | 184 | .settings: |
|---|
| | 185 | autoloading_functions: [[sfZendFrameworkBridge, autoload], [sfEzComponentsBridge, autoload]] |
|---|
| | 186 | |
|---|
| | 187 | What will happen when you create a new object of an unloaded class is: |
|---|
| | 188 | |
|---|
| | 189 | 1. The symfony autoloading function (`Symfony::autoload()`) first looks for a class in the paths declared in the `autoload.yml` |
|---|
| | 190 | 2. If none is found, then the callback functions declared in the `sf_autoloading_functions` setting will be called one after the other, until one of them returns `true`: |
|---|
| | 191 | 1. `sfZendFrameworkBridge::autoload()` |
|---|
| | 192 | 2. `sfEzComponentsBridge::autoload()` |
|---|
| | 193 | 3. If these also return `false`, then symfony will throw an exception saying that the class doesn't exist. |
|---|
| | 194 | |
|---|
| | 195 | Note that this setting is distinct from the rules defined in the `autoload.yml`. The `autoloading_functions` setting specifies bridge classes, and the `autoload.yml` specifies paths and rules for searching. |
|---|
| | 196 | |
|---|
| | 197 | The available bridges are stored in the `$pear_php_dir/symfony/addon/bridge/` directory. |
|---|
| | 198 | |
|---|
| | 199 | Plug-ins |
|---|
| | 200 | -------- |
|---|
| | 201 | |
|---|
| | 202 | Symfony can also be extended through plug-ins. To see how to install, use or create your own plu-in, refer to the [related chapter](plugin.txt). |
|---|