In this Tutorial, you will create your first object, which will be an instance of a physical address class. As you will be building your examples using this class, you will want to save it as you progress.
In your editor or IDE now, create a new file. The same PHP file-naming conventions apply to files that contain classes.
Many coding standards have file-naming and content conventions as well.
For readability, let's name the file containing the example class something logical: class.Address.inc.
I am using the extension INC, short for include, to indicate that this file is intended to be included and not executed on its own.
In PHP, a class definition always begins with the keyword class, followed by the name of the class. There are three PHP rules regarding the naming convention used for classes.
In particular, the class name must start with a letter or underscore; the remaining characters must only consist of letters, numbers, or underscores; and there is no limit on the length of the class name.
There are best practices for naming a class to aid with code readability and portability across multiple developers. Those conventions differ between coding standards, but a common theme tends to be the use of upper camel naming and avoiding the word class in the name.
Upper camel naming means the first letter of each word is capitalized. This is a variation of CamelCase, which does not specify whether or not the first letter should be capitalized.
This helps visually differentiate between an instance and an object.
Following the class name is a pair of curly braces that enclose any attributes, known as properties, and functions, known as methods, that belong to the class.
PHP allows the contents of the class to be empty,
Here are some practical examples of class creation in PHP.
This is valid as it starts with a letter, and the remaining characters are also
letters. class PhysicalAddress and class Physical_Address.
Each of these is valid and follows common best practices for capitalization. The use of underscores for word separation differs between coding standards.
class physical_address, but this time in lowercase.
This is valid but not a best practice, as each word does not start with a capital letter.
This class name is invalid.
It will trigger a parse error, expecting T_STRING because it starts with a number.