Welcome Guest, Not a member yet? Register   Sign In
Add namespaces in codeigniter 3
#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


Messages In This Thread
Add namespaces in codeigniter 3 - by rtorralba - 01-16-2015, 03:32 AM
RE: Add namespaces in codeigniter 3 - by mwhitney - 01-16-2015, 11:51 AM
RE: Add namespaces in codeigniter 3 - by mwhitney - 01-20-2015, 08:54 AM
RE: Add namespaces in codeigniter 3 - by sv3tli0 - 01-20-2015, 09:23 AM
RE: Add namespaces in codeigniter 3 - by qury - 12-04-2017, 04:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB