What is the best practise to save file which I will going to include in my other php
file should I save that file as myClass.php
or myClass.inc
There are some examples in which PHP uses .inc. It has no meaning, it is just a file extension. It is some people's convention to name files with a .inc extension if that file is designed to be included by other PHP files, but it is only convention.
It does have a possible disadvantage which is that servers normally are not configured to parse .inc files as php, so if the file sits in your web root and your server is configured in the default way, a user could view your php source code in the .inc file by visiting the URL directly, is insecure.
Its only possible advantage is that it is easy to identify which files are used as includes. Although simply giving them a .php extension and placing them in an includes folder has the same effect without the disadvantage mentioned above.
Almost any PHP coding standard requires a class file to have the same name as the class and the ending .php
.
class Test {
}
goes to Test.php
Namespaces or "pseudo namespaces" will be flattened to folders. Meaning
// pseudo namespace
class Foo_Test {
}
or
// "real" namespace
namespace Foo;
class Test {
}
goes to Foo/Test.php
.
You can refer to the
For historical reasons the .inc
extension is still deployed. In former times some people teached to "protect" php files which are meant for including but not for calling via url with this extension. Then they configured their .htaccess files to deny requests for .inc
files.
I've seen many web applications which missed the .htaccess part of that protection which leads to situations where you can get the source code (!) via the browser.
Modern web applications don't do that. The whole source directory is usually in a folder below document root and therefore not accessible or the whole folder is protected by a .htaccess rule. Accessible via the browser are only the so called front controller. This makes much more sense.
I think a good practice is if you have a class called "MyThing" in the code, to name it with that name, and the word "class". So for example:
class MyThing
{
}//END MYTHING
Would be in the file:
/includes/MyThing.class.php
use .php
extension! to use .inc
or any other extension you need to change in httpd.conf
(in apache) like
AddType application/x-httpd-php .php
to
AddType application/x-httpd-php .php .inc
Never use .inc for php code since that would allow other people to view your code (and possibly configs, database credentials, etc) by requesting your class files
You should definitely read PSR-4 which is current standard for autoloaders. It states what is preferred way of structuring your code:
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-meta.md https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
Basically you always name your file like your namespace and class, like this:
Class myThing
goes to {prefix}/myThing.php
Class myThing
from namespace Drawer
goes to {prefix}/Drawer/myThing.php
Class myThing
from namespace Room\Drawer
goes to {prefix}/Room/Drawer/myThing.php
and so on
In case you wonder, prefix
is a directory where you keep your classes, it may be anything: your project directory, your module directory, some includes
directory etc