PSR-0, PSR-4 & Composer
PHP-SAIGON MEETUP
08 December 2014
Hong Viet Nguyen
Autoloading in PHP
Why & How
- magical function __autoload
http://php.net/manual/en/function.autoload.php
- Before PHP 5.3: PEAR Coding Standard
http://pear.php.net/manual/en/standards.naming.php
Example: 'Zend_Db_Table' => 'Zend/Db/Table.php'
- From PHP 5.3: Namespace
PSR-0: Autoloading Standard
http://www.php-fig.org/psr/psr-0/
- Structure:
\<Vendor Name>\(<Namespace>\)*<Class Name>
- Namespace separator is converted to DIRECTORY_SEPARATOR when loading
- Backwards compatibility for PEAR-standard classnames
Examples
\Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php
\namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
Problems
vendor/
vendor_name/
package_name/
src/
Vendor_Name/
Package_Name/
ClassName.php # Vendor_Name\Package_Name\ClassName
tests/
Vendor_Name/
Package_Name/
ClassNameTest.php # Vendor_Name\Package_Name\ClassNameTest
vendor/
vendor_name/
package_name/
src/
Vendor_Name/
Package_Name/
ClassName.php # Vendor_Name\Package_Name\ClassName
tests/
Vendor_Name/
Package_Name/
ClassNameTest.php # Vendor_Name\Package_Name\ClassNameTest
Sometimes folder structure becomes too deep and duplicated, especially when using with Composer
PSR-4: Improved Autoloading
vendor/
vendor_name/
package_name/
src/
ClassName.php # Vendor_Name\Package_Name\ClassName
tests/
ClassNameTest.php # Vendor_Name\Package_Name\ClassNameTest
- Allow a more concise folder structure
- Drop backwards compatibility
- For now PSR-0 is officially deprecated
Examples
Problems
Do not show the exact path of a class (opposed to PSR-0)
Composer
What is Composer?
- A tool for dependency management in PHP
1. You have a project that depends on a number of libraries
2. Some of those libraries depend on other libraries
3. You declare the things you depend on
=> Composer finds out which versions of which packages need to be installed, and downloads them into your project
Why Composer?
- Avoid reinventing the wheel
- Third-party manager for dependencies
- Easy to use?
- Composer vs PEAR
Install
Local:
curl -sS https://getcomposer.org/installer | php
To use Composer, run:
php composer.phar
Global:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
To use Composer, run:
composer
composer.json
- Setup project: define all dependencies
- Located in project root directory
{
"require": {
"laravel/framework": "4.0.*"
}
}
Packagist
https://packagist.org/ - the main Composer repository
Installing dependencies
composer install
- Create composer.lock
- Each installed package will have a package definition located in its root directory
composer.lock
- The lock file: list versions of all installed packages
- Created by composer install, updated by composer update
- Both composer.json and composer.lock should be stored in version control
Autoloading
- Composer helps to set up autoloading
<?php
require 'vendor/autoload.php'; // include this in your bootstrap process
- Define our autoloader in composer.json
{
"require": {...},
"autoload": {
"psr-4": {"MyApp\\": "src/"}
}
}
References
http://php.net/manual/en/language.oop5.autoload.php
http://www.slideshare.net/lonelywolf/php-psr-standard-2014-0122
http://www.sitepoint.com/autoloading-and-the-psr-0-standard/
http://www.sitepoint.com/battle-autoloaders-psr-0-vs-psr-4/
http://code.tutsplus.com/tutorials/psr-huh--net-29314
http://www.slideshare.net/Seldaek/dependency-management-with-composer
http://www.slideshare.net/jasongr/composer-23263197
http://nelm.io/blog/2011/12/composer-part-1-what-why/
Q & A
Thank you !
PSR-0, PSR-4 and Composer
By Hong Viet Nguyen
PSR-0, PSR-4 and Composer
- 1,973