简体   繁体   中英

namespaces and class extends

Im trying to figure out how namespaces works in PHP, but havent really been lucky

Hope somebody could tell me what Im doing wrong here :)

code

require_once 'Vatcode.php';
$Vatcode = new \resource\Vatcode();

Vatcode.php

namespace resource;

require_once Ini::get('path/class').'/Resource.php';

class Vatcode extends Resource {
    public function __construct(){
        echo 'works!';
    }
}

Rescource.php

namespace resource;

class Resource {

}

error

Fatal error:  Class 'resource\Ini' not found in Vatcode.php

it's just a problem of namespace. Your class Vatcode is in namesapce ressource. If, in the file of VatCode declaration you use nameofclas::... or new nameofclass() it will try to get the class in namespace ressource. If you want to use the class Ini inside your document you have two solutions :

first give the full qualified name :

require \namespace\of\ini\Ini::get('path/class').'/Resource.php';

second using the "use" keyworld before using the get method :

use \namespace\of\ini\Ini;
require_once Ini::get('path/class').'/Resource.php';

In any case, if Ini is in "no namespace" ( global namespace is the accurate word) you just has to use the solutions I gave you but only with \\Ini instead of \\namespace\\of\\ini\\Ini

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