Use inside class versus outside class

Recently, I've had to put Use inside a class but I've always seen it put outside the class. What's the difference? When do I need to put it inside and when do I need to put it outside? If I put it outside, I get an error saying that I have an undefined method.

class MY_Controller extends CI_Controller
{
	use Auth;
}

Open in new window


use Auth;
class MY_Controller extends CI_Controller
{
}

Open in new window

Zoe ZoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
I suspect Auth is a trait

Refer to the documentation here

http://php.net/manual/en/language.oop5.traits.php
0
Kanti PrasadCommented:
Hi


The purpose of USE is to  bring one class from another namespace, but functionally there is no difference. Using USE will help you change your implementation with ease as you only need to make changes in few places only. Also if the namespaces path is long then it might be difficult to type the whole path each time when one need to instantiate that class.
0
Ray PaseurCommented:
The keyword "use" is overloaded in PHP and can have more than one meaning.  Here are some of the references.  In my opinion, multiple inheritance is an anti-pattern with lots of potential for confusion among team members.  I recommend avoiding it.  Namespaces, OTOH reduce the risk of confusion.  And auto-loading is a necessity.

http://php.net/manual/en/language.namespaces.basics.php
http://php.net/manual/en/language.namespaces.faq.php
http://php.net/manual/en/language.oop5.traits.php
http://php.net/manual/en/function.trait-exists.php
http://php.net/manual/en/language.oop5.autoload.php

"Use" is also part of that curious admixture of scopes in PHP's implementation of lambda functions.

http://php.net/manual/en/functions.anonymous.php
http://php.net/manual/en/class.closure.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.