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::...ornew 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 :
second using the “use” keyworld before using the get method :
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
\Iniinstead of\namespace\of\ini\Ini