Question

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.

Was it helpful?

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 lets 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 {

}

OTHER TIPS

For your understanding, autoloading works by registering the autoload function in the autoload stack via spl_autoload_register; this allows the function to be invoked whenever a new Class() statement is executed (more info).

On the other hand, the FIG standard for autoloading, PSR-0, defines how a namespace will be translated into a filename by a PSR-0 autoloader function. For example, given the namespace Vendor\Foo, the autoloader will look for the file Vendor/Foo.php having the following code

namespace Vendor;
class Foo
{
    public function do()
    {
        echo "Foo";
    }
}

Therefore, following the mandatory requirements, a PSR-0 compliant namespace resolves to the correct PHP file which could otherwise have been included using a require or include.

If I read your intentions correctly, you just need the following namespace in both code snippets:

namespace Employee;

Of course, this is not a PSR-0 namespace because there is no vendor name (unless your vendor name is 'Employee'). Anyway, using this namespace in your two code snippets will work fine.

However, if you intended to keep them in separate namespaces, then the autoloader won't figure out Department in extends Department in the second snippet. You will have to either import the namespace or explicitly specify it as so:

namespace Employee\community;

class Community extends Employee\Department\Department
{
  //code
}

I imagine that you did not expect the full class names from your snippets to be Employee\Department\Department, and that is why I first suggested keeping the same namespace for your purposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top