Wednesday, January 29, 2014

Re: What's the correct method for using Traits and Namespaces for CakePHP 2?

Just load the trait with App::uses()

On Tuesday, January 28, 2014 1:52:07 AM UTC+1, Reuben wrote:
My original question on Stack Overflow [http://stackoverflow.com/questions/21394852/whats-the-correct-method-for-using-traits-and-namespaces-for-cakephp-2], and content copied here, should that disappear.

I'm using CakePHP 2.4.5 and PHP 5.5, and would like to use a trait.

I have a trait in Utility/VariablesTrait.php called VariablesTrait.

To take advantage of namespaces, I've given it a namespace of App\Utility\VariablesTrait, since Utility\VariablesTrait seems a bit too global, and the former would work better with CakePHP 3.

In my class that I want to use it in, I have the use App\Utility\VariablesTrait; statement in the class. For backup, I also have a App::uses('VariablesTrait', 'Utility'); statement at the top of the file. I'm not sure if the SPL autoloader is used when looking for traits, which is why I was going for namespaces in the first place.

The small issue is that the app directory is app, and since directory structures should match namespaces (I think), I renamed it to App. However, CakeRequest::_base() hardcodes app, so determining the controller doesn't work so well.

So, I'm trying to determine if that's a CakePHP bug, or if there is a more appropriate way of using traits in CakePHP 2.


Regards

Reuben Helms

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: