[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

 
Google tung Majel đối đầu với trợ lí Siri

Google tung Majel đối đầu với trợ lí Siri

Google đang gấp rút xây dựng phần mềm ra lệnh bằng giọng nói Voice Actions trên Android với tên mã Majel nhằm tăng cường sức cạnh tranh với Apple.

Cách tạo server để truy cập trang web trong mạng nội bộ

Cách tạo window server để truy cập trang web trong mạng nội bộ

Trong một số trường hợp cần thực hiện một website chỉ dành riêng cho các máy tính trong hệ thống  mạng nội bộ truy cập để làm việc dựa trên một ứng dụng web chuyên nghiệp hoặc đơn giản là chỉ dùng để phổ biến thông tin nội bộ.

SEO website

Tối ưu SEO website với 5 bước

Theo nghiên cứu, 86% các truy cập website thông qua tìm kiếm. Nếu bạn làm SEO để website đứng đầu trong 20 kết quả đầu tiên khi người dùng tìm kiếm với các nội dung liên quan sẽ là một phần thưởng xứng đáng.

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

 

Diet con trung