[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

 
PSR là gì? Tìm hiểu chuẩn các PSR-0, PSR-4

PSR là gì? Tìm hiểu chuẩn các PSR-0, PSR-4

PSR stands for PHP Standards Recommendation - these are documents which lay out how the PHP community has agreed things will be done

Dù muốn hay không, người dùng Facebook vẫn phải sử dụng giao diện Timeline!

Dù muốn hay không, người dùng Facebook vẫn phải sử dụng giao diện Timeline!

Facebook vừa thông báo trên trang Blog của mình rằng giao diện Timeline sẽ áp dụng cho tất cả người dùng trong vòng vài tuần nữa.

Custom website bằng WordPress dễ hơn Drupal, Joomla

Custom website bằng WordPress dễ hơn Drupal, Joomla

Hôm nay COME™ Blog xin so sánh 03 CMS này để các bạn có cái nhìn rõ ràng hơn để lựa chọn mã nguồn phù hợp nhất cho website và nhu cầu của mình:

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

 

Diet con trung