• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » PHP » Defining a class in php
Next →
← Prev

Defining a class in php

By Dinesh Thakur

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.

class Address.

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.

class 21_jump_street.

This class name is invalid.

It will trigger a parse error, expecting T_STRING because it starts with a number.

You’ll also like:

  1. Defining Your Own Exception Class Java Example
  2. c++ – Defining member functions inside or outside the class definition
  3. What is an Object and class in PHP?
  4. Defining Structures in C++
  5. PHP Cookies
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

PHP Tutorials

PHP Tutorials

  • PHP - Home
  • PHP - Features
  • PHP - Magic Methods
  • PHP - Imagefilter
  • PHP - Arrays Numeric
  • PHP - Sessions
  • PHP - Forms Processing
  • PHP - clone()
  • PHP - Cookies
  • PHP - Variable Types
  • PHP - First program
  • PHP - call()
  • PHP - Iterator interface
  • PHP - Imports files
  • PHP - Exception Handling
  • PHP - set() and get()
  • PHP - Install MAMP
  • PHP - Functions
  • PHP - Constants Types
  • PHP - Comments Types
  • PHP - OOP's
  • PHP - OOps Use
  • PHP - PHP Code & Redirect 301
  • PHP - Control structures
  • PHP - Abstract Classes
  • PHP - Control structures
  • PHP - Classes
  • PHP - MySQL NULL values
  • PHP - Methods Visibility
  • PHP - Operator Types
  • PHP - Short tags Not use
  • PHP - Object and class
  • PHP - Secure Passwords

Other Links

  • PHP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW