clarkk
clarkk

Reputation: 27689

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

Upvotes: 0

Views: 1017

Answers (1)

artragis
artragis

Reputation: 3713

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

Upvotes: 3

Related Questions