php - Instantiate Class using namespaces -


i have function file, in put __autodload() function. classes want use namespaces, since i'm learning them. in folder lib/ have db class , test-user class. code of reads follows:

db.class.php

<?php namespace app\lib;  class db {     public $db;      function __construct($host, $username, $password, $database)     {         $this->db = new mysqli($host, $username, $password, $database);         if (mysqli_connect_error()) {             die('connect error');         }     } } 

user.class.php

<?php  namespace app\lib;  class user {     private $_db;      function __construct($db)     {         $this->_db = $db;     }      public function test()     {         echo '<pre>';         print_r($this->_db);         echo '</pre>';     } } 

at moment, index file looks this:

<?php  include 'functions.inc.php';  $db = new db('localhost', 'root', '', 'test'); $user = new user($db); $user->test(); 

and i'm getting error fatal error: class 'db' not found in c:\xampp\htdocs\tplsys\index.php on line 5

i've tried $db = new \app\lib\db() , $db = new app\lib\db(), it's same result... autoload function looks this:

function __autoload($classname) {     if (!file_exists('lib/' . $classname . '.class.php')) {         return false;     } else {         require_once 'lib/' . $classname . '.class.php';     } } 

edit

i got working , links provided sectus. code looks this:

index.php

<?php include 'functions.inc.php';  spl_autoload_register('autoload');  $db = new \lib\db('localhost', 'root', '', 'test'); $user = new \lib\user($db); $user->test(); 

autoload function:

function autoload($classname) {     $classname = ltrim($classname, '\\');     $filename = '';     $namespace = '';     if ($lastnspos = strrpos($classname, '\\')) {         $namespace = substr($classname, 0, $lastnspos);         $classname = substr($classname, $lastnspos + 1);         $filename = str_replace('\\', directory_separator, $namespace) . directory_separator;     }     $filename .= str_replace('_', directory_separator, $classname) . '.class.php';      require $filename; } 

classes:

<?php  namespace lib;  class db {     public $db;      function __construct($host, $username, $password, $database)     {         $this->db = new \mysqli($host, $username, $password, $database);         if (mysqli_connect_error()) {             die('connect error (' . mysql_connect_errno() . ') ' . mysqli_connect_error());         }     }  } 

i had rename namespace lib\ since app/lib/class.php otherwise... i'll have play bit. help, guys!

you must use class namespace namespaces or aliases.

<?php  include 'functions.inc.php';  $db = new db('localhost', 'root', '', 'test');// db class has no namespace $user = new \app\lib\user($db);               // <--- namespace added $user->test(); 

and function __autoload($classname) receive full class name (with namespace). so, when __autoload function execute recieve strings: 'db', 'app\lib\user'.

you should read psr-0 , use implementation instead of yours.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -