Inc and MVC extension in PHP

Asked

Viewed 676 times

3

I know you don’t have to use codenames inc and class in the name of the files, but because of the same organization is recommended.

The doubt is due to the fact of the codename class it is easy to understand that it is a file of classe, but the codename inc that is of include is confusing, because in MVC their use is ideial in the control directory or should be used only in files like autoload for example?

Because the way I see it, the files on control should also use this codename since they do includes, making the connection between the view and model.

2 answers

3

Not inc is not mandatory and is not a de facto standard, in fact there are better standards like the http://www.php-fig.org/psr/psr-4/, basically who uses Composer uses PSR-0 and PSR-4.

Another thing, MVC has nothing to do with with PHP or Web, MVC is a way to organize things and existed before the web applications and is not the only type of Pattern design that exists.

If you look at each popular framework that exists, which uses MVC to organize, each uses the "MVC" in its own distinct way, actually in most framework organizing following the Model, Controller and View is almost opitional in at least one of the cases, then in this fits your understanding that something that is not a "class" still can be MVC.

A detail, even if you want to use .inc this may make your scripts visible to those who navigate, because contracted servers and hosting usually do not have such an extension configured to be interpreted as a php script, so if one navigates like this:

http://site/foo/bar.inc

It will be able to see the source of your file, so anyone can know how your script was made and can even take advantage of this to get data as bank passwords or find security holes to exploit them.

In PSR-0 and PSR-4

The PSR-0 has been discontinued, I recommend only using the PSR-4, in both classes and frameworks fall within the default folder generally, and these folders are organized based on the class namespace, see this answer I cited as an example:

I’ll edit and put more links, there’s a lot here on the site about this

  • About existing other standards and MVC not having to do with web I already knew, my doubt was regarding the use of the inc extension with use of MVC, about the PSR I had already read some things, but I did not find any more complete content explaining the differences of each etc

2


File nomenclature is indifferent to MVC or any Patterns design.

Including ". inc" and ". class" was an old practice that even many older PHP programmers disagreed with the use.

Basically, these letters only helped to identify what kind of file it was. If it had the ". inc" in the nomenclature, meant that it was an inclusion file, ie should not run it directly. Ídem for the types ". class".

There are also the types ". func" and ". cons", for functions and constants respectively and probably there are others, but these are the best known.

Normally these files should be in a private access folder, not accessible by the user. But not everyone could follow this recommendation due to N reasons. Lack of knowledge or resources (the host did not allow).

In general, you will find nomenclatures of the type

file.inc.php
file.class.php
file.func.php
file.cons.php

Some omit the .php of the end, but this can be a bad idea because it can expose the codes in public if the environment is not well configured.

Old and widely used systems, such as Phpmyadmin, still use this practice.

It is worth noting that it is also not a bad practice. It is merely a matter of opinion/choice of who creates the project. The important thing is that the project is well written, well documented and organized. Nomenclature does not matter as long as you know what you are doing.

  • Perfect explanation, that’s exactly what I wanted to know, vlw

  • Golden answer! It’s a shame that so few people are interested in good practices.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.