[Drupal 8]: PSR-0 code trong Drupal 7 là gì?

[Drupal 8]: PSR-0 code trong Drupal 7 là gì?

Drupal 8 embraces modern PHP with all the trimmings, shedding the baggage of supporting earlier PHP versions and embracing the new object-oriented features.

One such feature is namespaced objects and the PSR-0 standard for autoloader compatability.

But if you know your project will run on a recent version of PHP, there's no reason you can't write your custom modules using PSR-0 now, in Drupal 7

What is a namespace

So first some background. What is a namespace? I was speaking with someone on irc recently and I quoted the fully-qualified name of a Drupal 8 object. Let's use \Drupal\image\Entity\ImageStyle as an example: their response was 'I freak out whenever I see those forward slashes'. But there is no need to freak out. Namespaces allow us to use the same class name more than once. Consider the Request class; in Drupal 8 there are three objects with a short-name Request as follows:

  • Symfony\Component\HttpFoundation\Request
  • Zend\Stdlib\Request
  • Guzzle\Http\Message\Request

In most cases you wouldn't need all three in the one file, you'd nominate which one you want to use with a use statement, eg. use Guzzle\Http\Message\Request and from then on that object can be simply referred to as Request. 

Therefore, if we didn't have namespaces, these objects would have to be given more verbose names such as Symfony_Component_HttpFoundation_Request and Zend_StdLib_Request to avoid clashing. You'd also have to use these full names every time you interacted with that class, which clearly would suck.

What is PSR-0

PSR-0 is an autoloader standard: if you define your class namespaces using the PSR-0 standard, they're compatible with other PHP libraries and code-bases that also follow PSR-0. PSR-0 ensures everyone gets along.

Autoloading in Drupal 7

Now in Drupal 7, we have our own autoloader. PSR-0 didn't exist as an accepted standard when Drupal 7 was developed, so instead we have our own autoloader that is based on modules declaring files to parse for objects in their info file using files[] entries.

X autoload - PSR-0 in Drupal 7

So how do you use PSR-0 namespaces in Drupal 7? Well the answer is simple, you use the excellent X autoload module by @donquixote. Just be sure to add it to your module's dependencies.

Once you enable the module, you can remove all of the files[] entries in your module's info file and rely on placing your classes in the correct PSR-0 folder structure instead.

So if your module is named robot and your fully-qualified class-name is Drupal\robot\Action\Destroy, you need to place the file containing the class in sites/all/modules/robot/lib/Drupal/robot/Action/Destroy.php, just like Drupal 8.

For an example of how much the module can clean up your info files, take a look at this example from views module. Granted it's an example using some of the other non PSR-0 features of X autoload but the difference is even more telling when you use PSR-0, as you have no files[] entries in your info file.

Disclaimer

It is highly likely that Drupal 8 will ship using the PSR-4 standard, instead of or in addition to PSR-0. The good news is xautoload also supports PSR-4, so when that change occurs in Drupal 8, it's a relatively simple change to make to your folder structure to move to PSR-4.

Bạn thấy bài viết này như thế nào?: 
Average: 9 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
“ Intel - Core i7-3960X Extreme Edition “ chính thức ra mắt năm 2012

“ Intel - Core i7-3960X Extreme Edition “ chính thức ra mắt năm 2012

Intel đã cho ra đời bộ vi xử lý mạnh nhất từ trước tới giờ - Core i7 Extreme Edition-3960X, cùng với một chipset mới, Express X79.

"Đổ thùng rác" tự động bằng AppleScript cho Mac

"Đổ thùng rác" tự động bằng AppleScript cho Mac

Thủ thuật này chỉ có hiệu nghiệm khi bạn áp dụng đoạn code, không phải là mọi lúc đều tự động xóa sạch nội dung Trash.

What Are The Best Apps For Iphone or Ipad?

Ứng dụng tốt nhất Đối với Iphone, Ipad là gì?

Communication has grown and phones have become a necessity to every body. There are different types of phones and individuals can choose from the variety offered.

Công ty diệt chuột T&C

 

Diet con trung