• 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class alias array config in future release of codeigniter

#1
There is great thing that codeigniter have built in composer auto load configuration by $config['composer_autoload'];

Codeigniter should provide class alias array which gives flexibility to access class by short handy name or by any other convenient name.
$config['class_alias'] =array(
    "ns1\ns2\ns3\A" => "A"
);
and you should access A class directly by 'A' instead of 'ns1\ns2\ns3\A'.
php have class_alias function as indicated on http://php.net/manual/en/function.class-alias.php.

Other famous framework like laravel and symfony are alredy using this idea..

This will make very easy to deal with any namespaced class.

I am in favour for namespace in codeigniter 4.
[email protected]
Php Team Leader, freelancer
github.com/pmbaldha
Reply

#2
I was thinking about the same thing few days ago.
My idea was related with easy way to extend core class as Router or other..

With providing 1 real and 1 public namespace for CI System.

The public namespace would be alias of the real but it will checks and configuration if there is some manual set alias which should overwrite the original..
Something as:

Router class..
class CodeIgniter/Router{} - The real Router class
class CI/Router{} - The alias class which will be used from developers inside their apps
class App/System/CustomRouter{} - The custom written Router which will be mapped as CI/Router inside the app configuration..
Best VPS Hosting : Digital Ocean
Reply

#3
A few thoughts:

class_alias, IIRC, will load the class into memory on every request, whether it's used or not. So that's not a good solution. What Laravel does is what they call a Facade, which is a magic layer that makes the commands look like simple, static class method calls, but they actually end up getting mapped through a core object that holds instances of the classes and calls it on that. Makes for a nice, clean syntax, bad on performance and a bit hellish when you're trying to debug your app at times. CI has always been more declarative, less magic. Don't expect to see Facades or anything similar show up in CI4.

When working with namespaced classes the easiest thing to do is to use a use statement at the top of the class that provides the full namespaced class. Then, we use access it in your class, you can just use the class name.

Code:
<?php

use \NS1\NS2\A;

class B {
    public function index ()
    {
        $var = new A();
    }
}

You can also use that to create local aliases for classes.

Code:
use \NS3\NS4\CDE as A;

class B {
    public function index ()
    {
        $var = new A();
    }
}

(12-27-2015, 01:56 AM)sv3tli0 Wrote: I was thinking about the same thing few days ago.
My idea was related with easy way to extend core class as Router or other..

With providing 1 real and 1 public namespace for CI System.

The public namespace would be alias of the real but it will checks and configuration if there is some manual set alias which should overwrite the original..
Something as:

Router class..
class CodeIgniter/Router{} - The real Router class
class CI/Router{} - The alias class which will be used from developers inside their apps
class App/System/CustomRouter{} - The custom written Router which will be mapped as CI/Router inside the app configuration..

It's an interesting thought to map one namespace to another and is something to consider. Though, I don't think it's necessary with the current system. If you want to override the Router (or most any part of the system) modify application/config/Services.php and let it know which class to use.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.