Welcome Guest, Not a member yet? Register   Sign In
Add namespaces in codeigniter 3
#1

(This post was last modified: 01-16-2015, 03:38 AM by rtorralba.)

Hi,


I think namespaces using will improve codeigniter structuration without make it more difficult its use.

For example current model loading way is some magic:

$this->load->model('users_model', 'users');

If someone read this php code, must to know that this sentence load Users_model class from models/users_model.php into $this->users only because is a codeigniter convenion

In addition to this, the IDEs need namespace information to helps developer in his work. 

Greetings.
Reply
#2

Please do a search before you post a topic like this. It has already been done ad nauseam.
Reply
#3

(01-16-2015, 03:32 AM)rtorralba Wrote: I think namespaces using will improve codeigniter structuration without make it more difficult its use.

As no1youknowz mentioned, this has been brought up several times. However, I would like to point out that what you appear to be saying here doesn't really have anything to do with namespaces. The structure of the code and of a given project or that of a project which uses the code doesn't have anything to do with namespaces. Namespaces just reduce the chance of name conflicts within code which must work together (and they don't prevent them, because you can easily create conflicts by using poor conventions in naming your namespaces).

(01-16-2015, 03:32 AM)rtorralba Wrote: For example current model loading way is some magic:

$this->load->model('users_model', 'users');

If someone read this php code, must to know that this sentence load Users_model class from models/users_model.php into $this->users only because is a codeigniter convenion

There is no magic here. If I wanted to know what was going on without reading the documentation and learning the basics of CodeIgniter, I would first need to know what $this->load refers to (which in most cases happens to be a class defined in system/core/Loader.php), then find the model() method on that class. It takes a little knowledge of PHP from that point to determine that the model method is loading the file specified ('users_model') and creating the specified alias ('users') on the CI object ($this->users) to refer to the created model.

Further, this process has nothing to do with namespaces, except for the fact that the model method in the loader was not designed to handle namespaced models (because namespaces did not exist in PHP when the loader was designed), so it may not be able to load your model if your model is in a namespace.

Nothing is preventing you from overriding the loader to handle namespaces or using a separate loader to load namespaced classes, though. Additionally, CI 3 already has some support for using Composer to load namespaced classes.

(01-16-2015, 03:32 AM)rtorralba Wrote: In addition to this, the IDEs need namespace information to helps developer in his work. 

This depends on your IDE (and, in most cases if your IDE has been designed to work well with PHP, maybe your configuration of that IDE). Since PHP has only supported namespaces for a very short amount of time (relative to the time PHP has been around), most IDEs and editors which support PHP have ways to determine which classes, methods, and properties should be available to your code without relying on namespaces. Namespaces can make this information more accurate for certain situations, but they're not required.

In a lot of ways, it seems like there are a lot of vocal people in the PHP community which equate namespaces with autoloaders, and fail to understand that they are two different concepts. In fact, many autoloaders give namespaces as much (or more) "magic" as you are placing upon CI's loader in your example.

All loaders are created for one simple purpose: abstracting away the messy details of calling include/require or include_once/require_once and dealing with the directory structure of your project. Namespaces are much more independent of these details than most people seem to realize, but making the directory structure reflect the namespaces makes things much easier for everyone, so most conventions make them mirror each other.
Reply
#4

(01-16-2015, 08:29 AM)no1youknowz Wrote: Please do a search before you post a topic like this.  It has already been done ad nauseam.

I'm sorry, i read some posts but i thought that doesn't was asking for the exactly same question.
Reply
#5

(This post was last modified: 01-16-2015, 05:53 PM by rtorralba.)

(01-16-2015, 11:51 AM)mwhitney Wrote:
(01-16-2015, 03:32 AM)rtorralba Wrote: I think namespaces using will improve codeigniter structuration without make it more difficult its use.

As no1youknowz mentioned, this has been brought up several times. However, I would like to point out that what you appear to be saying here doesn't really have anything to do with namespaces. The structure of the code and of a given project or that of a project which uses the code doesn't have anything to do with namespaces. Namespaces just reduce the chance of name conflicts within code which must work together (and they don't prevent them, because you can easily create conflicts by using poor conventions in naming your namespaces).







(01-16-2015, 03:32 AM)rtorralba Wrote: For example current model loading way is some magic:

$this->load->model('users_model', 'users');

If someone read this php code, must to know that this sentence load Users_model class from models/users_model.php into $this->users only because is a codeigniter convenion

There is no magic here. If I wanted to know what was going on without reading the documentation and learning the basics of CodeIgniter, I would first need to know what $this->load refers to (which in most cases happens to be a class defined in system/core/Loader.php), then find the model() method on that class. It takes a little knowledge of PHP from that point to determine that the model method is loading the file specified ('users_model') and creating the specified alias ('users') on the CI object ($this->users) to refer to the created model.

Further, this process has nothing to do with namespaces, except for the fact that the model method in the loader was not designed to handle namespaced models (because namespaces did not exist in PHP when the loader was designed), so it may not be able to load your model if your model is in a namespace.

Nothing is preventing you from overriding the loader to handle namespaces or using a separate loader to load namespaced classes, though. Additionally, CI 3 already has some support for using Composer to load namespaced classes.







(01-16-2015, 03:32 AM)rtorralba Wrote: In addition to this, the IDEs need namespace information to helps developer in his work. 

This depends on your IDE (and, in most cases if your IDE has been designed to work well with PHP, maybe your configuration of that IDE). Since PHP has only supported namespaces for a very short amount of time (relative to the time PHP has been around), most IDEs and editors which support PHP have ways to determine which classes, methods, and properties should be available to your code without relying on namespaces. Namespaces can make this information more accurate for certain situations, but they're not required.

In a lot of ways, it seems like there are a lot of vocal people in the PHP community which equate namespaces with autoloaders, and fail to understand that they are two different concepts. In fact, many autoloaders give namespaces as much (or more) "magic" as you are placing upon CI's loader in your example.

All loaders are created for one simple purpose: abstracting away the messy details of calling include/require or include_once/require_once and dealing with the directory structure of your project. Namespaces are much more independent of these details than most people seem to realize, but making the directory structure reflect the namespaces makes things much easier for everyone, so most conventions make them mirror each other.

Hi,

I agree and known your namespaces explanation.

I meant what you said in this paragraph:
Quote:Further, this process has nothing to do with namespaces, except for the fact that the model method in the loader was not designed to handle namespaced models (because namespaces did not exist in PHP when the loader was designed), so it may not be able to load your model if your model is in a namespace.

Namespaces doesn't existed when loader was designed for codeigniter 2, I talked about new features for codeigniter 3.
I know exactly how loader class works, simply i want to say that code like other frameworks i seem more clear and readable, maybe also more repetitive.

E.g.:

Code:
Yii:

$model = new \app\models\Users;

Codeigniter:

$this->load->model("users_model", "users")

It was just an opinion.

I like codeigniter framework and i like its simplicity and ease, and i want that it will kept equal. just i think can will add some concepts like composer use, that not will make it more difficult.

I think too, that is difficult establish how many new concepts to add to improve codeigniter without make it too much complicated and increase his bootstrap work.

Greetings.
Reply
#6

(01-16-2015, 05:00 PM)rtorralba Wrote: Hi,

I agree and known your namespaces explanation.

I meant what you said in this paragraph:

Quote:Further, this process has nothing to do with namespaces, except for the fact that the model method in the loader was not designed to handle namespaced models (because namespaces did not exist in PHP when the loader was designed), so it may not be able to load your model if your model is in a namespace.

Namespaces doesn't existed when loader was designed for codeigniter 2, I talked about new features for codeigniter 3.

CodeIgniter 3 has been in development for a long time and is nearly ready for release. At this point, they're not really looking at new features for CI 3.

(01-16-2015, 05:00 PM)rtorralba Wrote: I know exactly how loader class works, simply i want to say that code like other frameworks i seem more clear and readable, maybe also more repetitive.

E.g.:


Code:
Yii:

$model = new \app\models\Users;

Codeigniter:

$this->load->model("users_model", "users")

It was just an opinion.

I like codeigniter framework and i like its simplicity and ease, and i want that it will kept equal. just i think can will add some concepts like composer use, that not will make it more difficult.

I think too, that is difficult establish how many new concepts to add to improve codeigniter without make it too much complicated and increase his bootstrap work.

Greetings.

There is nothing stopping you from using $model = new \app\models\Users; in CodeIgniter 2 or 3. However, if you haven't included the file which defines the class \app\models\Users, you need an autoloader which can find the class. If you want to use Composer to do this, you can, and CI 3 includes a configuration option to automatically load Composer if you don't want to load it yourself.
Reply
#7

Its already too late for namespaces at v.3 perhaps in future version we can get namespaces with singelton and multiple instances...
Best VPS Hosting : Digital Ocean
Reply
#8

HI, you can do the following:

1, enable auto loading composer in config.php
2, add the below to your composer.json
PHP Code:
   "autoload": {
 
       "psr-4": {
 
           "App\\""application/"
 
       }
 
   
3, namespace your model
PHP Code:
<?php namespace App\Models;

class 
Testm extends \CI_Model
{
 public function 
test()
 {
 return 
"Hello World!";
 }


4, now you can use the below in your controller:
PHP Code:
public function somefunction(){
 
       
        $test
= new \App\Models\Testm();

 
        echo $test->test();


This is picked up correctly by netbeans. I do not know about the other editors/DEs.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB