Namespace in PHP CodeIgniter Framework
Asked Answered
H

5

18

Does CodeIgniter support Namespace?

Hoyt answered 13/9, 2010 at 13:4 Comment(1)
A better question to ask is why do you need them? I generally "namespace" code with prefixes or suffixes. For example, $this->user_lib or $this->user_m means you wont clash with anything else. I'd REALLY like CI to support Controller_Foo but thats not gonna happen soon :-/Monkish
S
22

Namespace are supported by php and not by the framework (codeigniter in your case). If you use namespaces php version must be >= 5.3.0 Codeigniter dosen`t use namespaces because it is written to support php 4.

Stallfeed answered 13/9, 2010 at 13:8 Comment(1)
Here is a workaround found: porquero.blogspot.com/2012/03/…Amosamount
F
35

How To Get Namespaces to Work in Codeigniter

Actually, you can get namespaces to work in conjunction to relative paths in your application models. This modification makes loading models much easier and also allows you to have interfaces...

Add this to the end of your application/config/config.php

spl_autoload_extensions('.php'); // Only Autoload PHP Files

spl_autoload_register(function($classname) {
    if (strpos($classname,'\\') !== false) {
        // Namespaced Classes
        $classfile = strtolower(str_replace('\\', '/', $classname));

        if ($classname[0] !== '/') {
            $classfile = APPPATH.'models/' . $classfile . '.php';
        }               
        require($classfile);
    } elseif (strpos($classname, 'interface') !== false) {
        // Interfaces
        strtolower($classname);
        require('application/interfaces/' . $classname . '.php');
    }
});

Example Namespaced Class:

<?php
// File: application/models/foo/bar.php
namespace foo;

class Bar extends \CI_Model implements \Awesome_interface {

    public $foobar;

    public function __construct() {
        return parent::__construct();
    }

    public function getFoobar() {
        return $this->foobar;
    }

    public function setFoobar($val) {
        $this->foobar = $val;
    }

}

Example Instantiation of Class in Your Code Somewhere:

IMPORTANT NOTE: DO NOT USE BUILT IN CI_Loader ( Ex: $this->load->model(); )

// This will Autoload Your Namespaced Class
$example = new foo\Bar();

or alternatively on top of your PHP class (ex: controller, other model), you can do this...

<?php
...
use foo\Bar as FooBar;

...

// Then you can just do this
$example = new FooBar();

Example of Interface:

<?php
// File: application/interfaces/awesome_interface.php
interface Awesome_interface {

    public function getFoobar();

}
Feudal answered 18/2, 2014 at 15:43 Comment(5)
after a couple of changes to the spl_autoload_register method classes load without error. ThxPlow
@TimothyPerez Is there any other way to load libraries having namespace in CodeIgniter 3 ?Stripling
@JunaidQadirShekhanzai after a couple of changes ? What changes did you make ?Comedietta
It's been years and I don't remember. SorryPlow
3.x supports namespaces. I have moved to LaravelPlow
S
22

Namespace are supported by php and not by the framework (codeigniter in your case). If you use namespaces php version must be >= 5.3.0 Codeigniter dosen`t use namespaces because it is written to support php 4.

Stallfeed answered 13/9, 2010 at 13:8 Comment(1)
Here is a workaround found: porquero.blogspot.com/2012/03/…Amosamount
R
5

Just a simple psr-4 autoloading and you are done.

In the config/config.php load composer using

$config['composer_autoload'] = FCPATH.'vendor/autoload.php';

In the root directory run composer install In the generated composer.json add following lines for psr4 autoloading.

    "autoload": {
        "psr-4": {
            "App\\": "application/"
        }
    },  

App would be your namespace in this case.

Example: suppose you have a class Service in libraries folder. You can namespace it with:

<?php
namespace App\libraries;

class Service{
}

Use it in Welcome controller class:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use App\libraries\Service;
class Welcome extends CI_Controller {
}
Rambouillet answered 16/9, 2019 at 5:42 Comment(0)
L
1

You could check this out: yidas/codeigniter-psr4-autoload

The lib defines app as CI application root so that every classes in application could be loaded with PSR-4 namespace:

\app\libraries\MemberService::auth();
\app\helpers\ArrayHelper::indexBy($input);
\app\widgets\StatWidget::run();
class Blog_model extends app\core\BaseModel {}
class Car_model implements app\contracts\CarInterface {}

Sample code for defining a class:

<?php
namespace app\helpers;
class ArrayHelper
{
    public static function indexBy($input) {}
}

https://github.com/yidas/codeigniter-psr4-autoload

Larimore answered 16/1, 2018 at 8:40 Comment(0)
A
-1

add the line below in a class that you want to use goutte`

require __DIR__ . "/../../../vendor/autoload.php";

and in absolute path of your project run :`composer

require fabpot/goutte

to add goutte in your package.json file. now all things should work well.`

Arise answered 13/8, 2022 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.