Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Does a class need to have the same name as the file?

Tags:

oop

php

class

Does a class need to have the same name as it's file.

Ex. class.mysql.php and inside it having class mysql (same)

Ex. class.fish.php and inside it having class mysql (diff)

Also can the format for a class file name be name.class.php or does it have to be class.name.php?

Thank You

like image 566
KRB Avatar asked Jun 21 '11 02:06

KRB


People also ask

Does a class have same name as file?

The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point. Suppose when we create a program in which more than one class resides and after compiling a java source file, it will generate the same number of the .

How do you name a class in PHP?

Class names are always written in UpperCamelCase . The unqualified class name must be meant literally even without the namespace. Class names must be nouns, never adjectives. The name of abstract classes must start with the word “Abstract”, class names of aspects must end with the word “Aspect”.

Does the class name have to be the same as the file name java?

In Java, the java file name should be always the same as a public class name. While writing a java program first it is saved as a ". java" file, when it is compiled it forms byte code which is a ".

Should class names be capitalized PHP?

Class names should be descriptive nouns in PascalCase and as short as possible. Each word in the class name should start with a capital letter, without underscore delimiters. The class name should be prefixed with the name of the “parent set” (e.g. the name of the extension) if no namespaces are used.


1 Answers

There is no link between the file name and the class name, you can name the classes in a file anything you want, have multiple classes or have no classes at all. However, it is a good idea to develop a consistent convention to indicate what the file contains via its file name.

Edit:

See Jason McCreary's answer below as some of these conventions have changed.

like image 64
GWW Avatar answered Oct 02 '22 22:10

GWW