简体   繁体   中英

How to extend PHP class in mulitple files and mark every extend in namespace?

I have the following code:

File: class.engine.php (The Main Engine of the website)

<?php
namespace Core;

class Engine {

}

File class.errors.php (The Error trigger if something happens, customized errors)

<?php
namespace Core\Errors;

class Errors {
    public function TrigerError($e)
    {
        die($e);
    }
}

File class.mysqli.php (The database connector)

<?php
namespace Core\MySQLiDrv;

class MySQLiDrv {
    public $db = null;

    public function __construct($database) {
        $connector = mysqli_connect($database[0], $database[1], $database[2], $database[3]);
        if(!$connector)
        {
            return new TrigerError("1");
        }
    }
}

File index.php (Where everything runs)

<?php
include("engine/class.engine.php");
include("engine/class.errors.php");
include("engine/class.mysqli.php");

$engine = new Engine();
$db = new MySQLiDrv(array("localhost", "user", "pass", "db"));

I want to extend Engine class with many multiple files and I want them to be in namespace, how can I do that properly, because I can't trigger the error in this case of the code.

And is it important in this case of code to use namespaces defined as something for easy use?

Your main issue as I see it is the lack of the USE statement and the fact that TrigerError is a method and not a class.

To fix these:

namespace Core\MySQLiDrv;

use Core\Errors\Errors;

class MySQLiDrv {
    public $db = null;

    public function __construct($database) {
        $connector = mysqli_connect($database[0], $database[1], $database[2], $database[3]);
        if(!$connector)
        {
            return (new Errors)->TrigerError("1");
        }
    }
}

Full working code:

namespace Core{
    class Engine {
    }
}

namespace Core\Errors{
    class Errors {
        public function TrigerError($e)
        {
            die($e);
        }
    }
}

namespace Core\MySQLiDrv{

    use Core\Errors\Errors;

    class MySQLiDrv {
        public $db = null;

        public function __construct($database) {
            //$connector = mysqli_connect($database[0], $database[1], $database[2], $database[3]);
            //if(!$connector)
            //{
                return (new Errors)->TrigerError("1");
           //}
        }
    }
}

//default global namesapce
namespace {
    use Core\Engine;
    use Core\MySQLiDrv\MySQLiDrv;

    $engine = new Engine();
    $db = new MySQLiDrv(array("localhost", "user", "pass", "db"));

}

Output

1

Sandbox

The only real difference here is that when you use multiple namespaces in a single file (sandbox) you have to enclose the code with {} . You can remove those if these are separate files. Well that and I cant use the DB functions in the sandbox, but that is irrelevant in this example because we want to trigger the error.

However as said in the comments, I would look into Autoloading and Exceptions both of which will help you here.

I have an autoloader on github you can play around with if you want, it's similar to what Composer uses/does. Composer is just more standardized.

Autoloadig basically uses a combination of the namespace and class to find the file at run time (when the class is instantiated). So there are some naming conventions to be aware of, for example take your class Core\\Errors\\Errors this should be located in Core/Errors/Errors.php The namespace and the file path should be the same. What this does is you can eliminate all the include statements and this loads the files only when needed auto-magically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM