Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Adding a Basic Form Using Object and Validating Email

Why does "use" have to be the very first line before "require"

Alena never game a lesson on namespace so I took a quick youtube lesson ( https://www.youtube.com/watch?v=V0CtmYaSju8&t=171s ) I found very helpful and the tutorial used "require" BEFORE "use"

Out of curiosity I used "require" first and "use" last and the code still worked perfectly!

I am wondering if this lesson was taught before PHP7 was released and if perhaps if that was the reason?

Is it still best practice to have "use" as the very first line?

1 Answer

pouya yousefi
pouya yousefi
12,480 Points

The use operator is for, giving aliases to names of classes, interfaces or other namespaces. In my idea, it's best practice to use it in the very first line so we can are sure we can use aliases after the first line.

Thans Pouya, Does it need to be first line, or is it just that it is best practice?

pouya yousefi
pouya yousefi
12,480 Points

I think it's best practice.