phppsr-0

php psr autoload ambiguity


I am working on a php sdk rewrite project and the client wants to get PSR standards done. I am looking at the standards page here

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

One thing what i am not able to understand, if i use name spaces in my class do i still need to use include or require or use. I mean the whole reason of autoload beats the purpose right ?

For example, say i have a class this way,

namespace Employee\Department;

Class Department 
{
    //code
}

and i have another class which uses this class by extending it,

namespace Employee\community;

Class Community extends Department
{
   //code
}

so does the above code make it to psr-0 standard considering that i have an autoload function exactly thats on the link above.


Solution

  • The second example is going to assume Department is in the Community namespace so in this case you would need a use statement. Also both of your examples would use the namespace Employee not Employee\Whatever. For example, let's assume the following layout:

    Employee/
      Community.php
      Community/
         Manager.php
      Department.php
      Department/
         Manager.php
    

    Then we would see the class/namespaces like the following

    namespace Employee;
    
    class Department {
    
    }
    
    ///////////
    
    namespace Employee; 
    
    class Community extends Department {
    
    }
    
    /////////////
    
    namespace Employee\Department;
    
    class Manager {
    
    }
    
    /////////////
    
    namespace Employee\Community;
    use Employee\Department\Manager as BaseManager;
    
    Class Manager extends BaseManager {
    
    }