Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Autoloaders in PHP

96 views

Published on

Most often WordPress developers uses manual file loading by just typing require_once all the time. But this approach is not good for all cases. There is few moments which you should to know:
— Load each file manually. But what if you have over 400 files or more?
— Your code contains the logic of loading files. And you probably doing more work by designing this logic of loading files.
— Some methods and classes may missed (loaded too later).
— One more node for testing. You need to make sure that your loaders work as intended.
The good news — you can use autoloaders and this is not required something special. And this keynote about autoloaders and Composer.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Autoloaders in PHP

  1. 1. Автозагрузка в PHP WordPress Meetup #44 15 апреля 2017 Москва, Setka
  2. 2. Как было раньше Примеры. Минусы и недостатки Как надо делать Современный мир и решение проблем Практика Сложные и не очень примеры
  3. 3. Ручная загрузка
  4. 4. function hello_dolly_get_lyric() { /** These are the lyrics to Hello Dolly */ $lyrics = "Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when So, take her wrap, fellas Find her an empty lap, fellas Dolly'll never go away again Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when Golly, gee, fellas Find her a vacant knee, fellas
  5. 5. /** * Implement the Custom Header feature. */ require get_parent_theme_file_path('/inc/custom-header.php');
  6. 6. $authentic_includes = array( '/lib/init.php', // Initial Theme Setup '/lib/plugins.php', // List of Required Plugins '/lib/dashboard.php', // Dashboard Functions '/lib/options.php', // Options '/lib/theme-mods.php', // Theme Mods '/lib/custom-fields.php', // Custom Fields '/lib/template-tags.php', // Functions '/lib/assets.php', // Theme Assets '/lib/gallery.php', // Gallery ); foreach ($authentic_includes as $file) { include_once get_template_directory() . $file; }
  7. 7. $functions = scandir(get_template_directory() . '/functions/');
 foreach ($functions as $key => $function) {
 if ( $function != '.' && $function != '..' ) require_once get_template_directory() . '/functions/' . $function;
 }
  8. 8. Зачем нужна автозагрузка? 1.Любой код (файл) доступен в любое время. 2.Меньше логики. 3.Универсальность. 4.Совместимость с другими проектами. 5.Меньшая вероятность коллизий. 6.Лучшая структура и красивые имена.
  9. 9. require_once 'vendor/autoload.php';
  10. 10. Использование
  11. 11. • Classmap ~ WordPress • PSR-4 ~ Symfony • Отдельные файлы с функциями ~ Twig
  12. 12. {
 "name": "symfony/validator",
 "type": "library",
 "autoload": {
 "psr-4": {
 "SymfonyComponentValidator": ""
 }
 }
 } composer.json
  13. 13. Пример из мира WordPress
  14. 14. Коля Коробочкин WordPress разработчик korobochkin@setka.io

×