0

I have following library structure

C:\WWW\WEBS
|   users.php
|
\---lib
    |   Api.php
    |   Server.php
    |   TimeSync.php
    |
    \---TimeSync
            Ntp.php
            Protocol.php
            Sntp.php

In server.php I have

<?php
namespace lib; 
class Server extends Api
{
}
?>

In users.php I am using it as

<?php
  use lib\Server;
  $objServer = new Server();
?>

I also tried it using like use lib\Server;

But in both cases it is saying

Fatal error: Class 'lib\Server' not found in

C:\www\Experimentation\webserviceserver\users.php on line 7

Where I am going wrong .Should I user a autoloader?

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
  • Are you requiring or including the file somewhere? If not then yes, you can use an auto loaded to ensure it's loaded into memory. If that is in fact the entirety of your code, the problem is that the two files are completely unaware of each other. – Mark Feb 06 '15 at 16:06
  • What autoloader are you using? – Maks3w Feb 06 '15 at 16:10
  • @Mark I think I am misunderstanding the concept then .. some says the with php 5.3 and psr4 no autoloader is required .How can I write standard autoloader for psr4 ? – alwaysLearn Feb 06 '15 at 16:16
  • See here: http://stackoverflow.com/questions/13441564/php-5-3-autoloader – Mark Feb 06 '15 at 16:19

1 Answers1

0

You will need to use an autoloader. You can use the one delivered in Composer. Once you've installed it (at the root of your project), you put a composer.json file, and inside you copy the following code :

 {
    "autoload":{
        "psr-4":{
            "Lib\\" : "lib/"
        }
    }
 }

Then you use the command composer dumpautoload. That will copy the autoloading files in a folder named "vendor" at the root of your project.

Then in users.php you require the autoloader :

require_once "vendor/autoload.php"

And you use the namespacing :

use Lib;
$objServer = new Lib\Server();

It might work.