Capital Letters in Class Name PHP

Php files should have upper case?

The classname must be the same as the filename, as you've said. If you want to stick with the lower case filenames, then make the $class_name lowercase.

The function is strtolower()

$CorePath = dirname(__DIR__) . '/fod/' . strtolower($class_name) . '.php';

But the file must have the same name as the class, so that the autoloader can work.

PHP - spl_autoload and namespaces - doesn't work with capital letters

When you run your PHP code on a Linux platform, it's important to remember that Linux is case sensitive with filenames.

This affects autoloaders because they typically use the namespace and the class name when building the filename to load.

If the folder is named core, then the namespace must be core, with the same capitalisation. If you change it to Core in the namespace, then you must do the same to the folder name. (and as a result, all other core classes must be changed to Core at the same time).

On Windows, this doesn't happen because the Windows filesystem isn't case sensitive. This can cause confusion when code is tested and works on a local Windows-based dev system, and then breaks when it is copied to a Linux-based server.

[EDIT]

Okay, so I missed that you had changed the dirname as well. But nevertheless, I still think this is an issue of the filename/dirname case.

I note that you're calling spl_autoload_register() without any params. This means that the default spl_autoload() function will be used as the autoloader.

If you read the documentation for spl_autoload(), you'll note that it uses the lowercased version of the class and namespace.

In other words, using the default autoloader, your classes can be mixed case, but the folder structure and filenames must be all lower case.

So in fact, for you, you need to keep your filenames lower case.

I've personally experienced it the other way round, as per my original answer, where I had a fully lower case filename, and my mixed case class name was breaking when I moved from Windows dev box to Linux server. The reason my experience is different from yours is because I'm using a custom-written autoload function, which doesn't do an auto-lowercase conversion, so the case of my filenames has to match that of my classnames.

Should I capitalize constructor names for my classes?

Named constructors are PHP 4 convention. And yes, when you do it that way you should match the case of the class.

But PHP 4 is old news these days. PHP 5 has been the de-facto version for several years now. And PHP 5 uses a constant constructor name, __construct. It's always this, regardless of the class' name.

class a
{
public function __construct()
{}
}

class b
{
public function __construct()
{}
}

PSR-2. When to capitalize directories?

You can name your directories whatever you like.

However, if you intend to autoload your PHP classes with either PSR-0 or PSR-4, the directory names touched by the relevant class name part have to match exactly.

PSR-2 does not make any assumptions or does not give and rules on which case a classname or namespace has to have.

PSR-1 does state in chapter 3: "Class names MUST be declared in StudlyCaps." This however only affects the file name of the file containing such class. If you are using PSR-4 autoloading, you can still avoid using any part of the namespace at all in the path if you define a whole map of Name\Space -> directory/for/that/namespace for every directory that contains code.

PSR-1 does not have any rules about the formatting of the namespace, so you could also avoid using uppercase letters there and because of that avoid having to use uppercase letters in the directory path.

Note that both PSR-0 and PSR-4 autoloading are case sensitive when it comes to mapping a namespace/classname to a filesystem path and file. So in the end, you will end up having a case sensitive filesystem layout anyways.

Also note that most namespaces are also using StudlyCaps, and I'd consider using lowercase letters to be unusual.

autoloader with upper and lowercase classname

Gumbo's solution is the best one and that's what almost everyone uses. I do not understand why you do not like. Of course you can first check whether a file with the class name capitalized exists or not, and if not then check whether lowercased version exists or not. That's definitely not better than Gumbo's solution. After all "your" programmers have to follow some conventions/rules.

Uppercase for variable on PHP, good or not?

You are free to use whatever case you want.

Historically there have been several conventions. Some preferred uppercase, some lowercase, and yet some who preferred mixed case. Which you used usually depended on how you wrote your HTML, and what background you came from (some programming languages encouraged/required you to use uppercase, for instance, and because PHP is not picky about what case you use it was easy to just continue using what you knew).

Now-a-days there are a couple conventions. The following are generally what you will see when reading other people's code:

Most people use lowercase for variable and function names:

$name = "Some Name";
function age(Person $person) {
return $person->age;
}

As for names that contain more than one word there tends to be roughly two camps: Those who prefer camelCase, and those who prefer under_score:

$personName = "Some Name";
function getAge(Person $person) {
return $person->age;
}
// Or...
$person_name = "Some Name";
function get_age(Person $person) {
return $person->age;
}

For classes/interfaces/etc. most people will use the same convention as variables and functions, but uppercasing the first character. This is probably done in order to make it easier to differentiate them from other things like function names (i.e. functions start with lowercase, classes/interfaces/etc. start with uppercase). Multiple words are usually in CamelCase or in Under_Score.

Class names are often formatted in such a way to facilitate autoloading. Using underscores is a way to easily mirror the underlying folder structure (_ becomes /, i.e. Under/Score). CamelCase can also be used in the same manner by putting the folder separator before the second, and later, capitalised letters (i.e. Camel/Case).

With namespaces becoming more and more popular making the class name analogous to the file path has become less and less necessary (the namespace itself has largely taken on that role). Because it makes a certain kind of sense, and because the autoloading standards/conventions are still around, you will still see it, though.

class Person {}
class ComputerProgrammer extends Person {}
// Or...
class Computer_Programmer extends Person {}

For constants most people will use all UPPERCASE. This is to make constants look distinct from variables/function/etc. It is a convention that comes from the C language, which PHP is closely related to.

define("DB_PASS", "Pa5sW0Rd");

Related to constants, language constant such as booleans and null are usually in either all lowercase or all uppercase:

true
TRUE
false
FALSE
null
NULL


Related Topics



Leave a reply



Submit