Yii Framework Forum: Namespaces And Simplicity - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Namespaces And Simplicity

#1 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 12 May 2013 - 03:13 AM

Hello all,

I have a question about namespaces.. The PHP manual says the following:

Quote

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:
  • Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  • Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.


Yii2 currently uses "sub-namespaces", meaning something like:
namespace yii\helpers\base;



In my opinion, this doesn't alleviate the Extra_Long_Names problem. It might even make things more complex. ("Which namespace do I need to include??")

What about we have 3 namespaces?
  • yii
  • app
  • ext (this one would need sub-namespaces to avoid name collisions)


Then we could just do:
<?php

namespace app;

use yii;


Nice and simple! :) Thoughts?
0

#2 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 12 May 2013 - 08:24 AM

You are not a developer =).
0

#3 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 12 May 2013 - 08:36 AM

You can alias long namespaces by doing the following:

use yii\helpers\base as Base;

See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 12 May 2013 - 10:26 AM

When I develop in C++, I always take care to use the full namespace in the interface, especially when I program libraries to be used by others.
Haven't used it yet in PHP, but I think it's a good idea.
In the private implementation, I usually use 'using namespace long_namespace' for convenience.
"Less noise - more signal"
0

#5 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 12 May 2013 - 03:48 PM

View Postsensorario, on 12 May 2013 - 08:24 AM, said:

You are not a developer =).

Definitely true. :) I am also not very experienced in using namespaces. I am just curious what the thought process behind sub-namespaces is.
0

#6 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 13 May 2013 - 01:10 PM

View PostyJeroen, on 12 May 2013 - 03:48 PM, said:

Definitely true. :) I am also not very experienced in using namespaces. I am just curious what the thought process behind sub-namespaces is.


The idea behind sub-namespaces is to group related classes (and thus separating them from unrelated functionalities).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users