Naming Conventions And A Few Other Thoughts

I know i am late to this party and yii2 is where it is right now, but there’s some things that i can’t just help to say something about.

I am looking at namespacing and i see things like:




namespace app\controllers\Controller;


or 


new app\models\MySuperModel();


or


return \yii\helpers\Json::encode($items);



Why are you using lowercase namespaces while the class itself is camel case?

Wouldn’t the below look better:




namespace App\Controllers\Controller;


or 


new App\Models\MySuperModel();


or


return \Yii\Helpers\Json::encode($items);



All the other big frameworks use a consistent convention, but this one in yii2 is very weird, what is the rationale behind it? Is there a chance to be consistnt with this?

Also, i haven’t moved to yii2 because 1.1.x suffice my needs now and also i have multiple projects already in that version, but yesterday i took a look at the github code, and what struck me as a bit odd was the usage of array_key_exists() function. It is used in so many places when a simple isset($array[$key]) would suffice.

Why is that? Is like you are trying to make the framework eat more memory.

Please note that i am just trying to understand things, i don’t want to make anybody feel bad, it’s just i don’t understand and if i am going to switch to yii2 i need to know what’s under my code and why.

Thanks.

We had that discussion before and came to the conclusion that lower case for namespaces is just like we use lower case for directories. You see what part is the namespace and what part is the class. Did not find the forum topic about it, you may search yourself.

can you give an example? We normally use array_key_exists, when the array item may be null because isset would fail in this case.

Hi CeBE, thanks a lot for your reply.

For #1, okay i see the point but is that decision final?

Why not rename the dirs to start uppercased, zend does it, symfony does it too, so i don’t think is wrong at all, and this would be even better when we will mix Yii with zend or symfony components, it just makes sense to be so.

For #2: https://github.com/yiisoft/yii2/blob/master/framework/di/Container.php at

line 143 you do a "if (isset($this->_singletons[$class])) {"

and at

line 173 you do a "if (array_key_exists($class, $this->_singletons)) {"

I mean in this case, how could the class param be null, doesn’t really make sense to be so, albeit the first isset call.

This is just an example i found in a few seconds, i am sure that more search would reveal more of this kind of usage.

For #1: No, we will not change it. There is no reason to try being equal to others. We already adopted PSR standards, thats enough imo. If we would take everything from other frameworks, why do you need Yii then?

For #2: Check https://github.com/yiisoft/yii2/blob/master/framework/di/Container.php#L268 there is a good reason for using array_key_exists in this case.

If you want to check both (depends on the need): 1) if not set and 2) null/empty , this is shorter:




if (!empty($this->_singletons[$class])) {

}



#1 - Okay, i just wanted to know. It’s not about taking everything from the other frameworks, it’s just some code doesn’t look standardized, like the below:




namespace yii\di;


use Yii;

use yii\base\InvalidConfigException;



#2 - Yes it makes sense, thanks :)

Anyway, if naming convention isn’t going to change i assume we must live with it.

Thanks for your time.