Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial – Learn to create a PHP5 Class

PHP5 Tutorial – Learn to create a PHP5 Class

November 8th, 2007 Leave a comment Go to comments

Before we begin learning how to create PHP5 Class, lets first understand the meaning of a class in object oriented programming practices.

Definition of a Class

A class is user defined data type that contains attributes or data members; and methods which work on the data members. (You will learn more about data members and methods in following tutorials. This tutorial focuses only on learning how to create a Class in PHP5)

To create a class, you need to use the keyword class followed by the name of the class. The name of the class should be meaningful to exist within the system (See note on naming a class towards the end of the article). The body of the class is placed between two curly brackets within which you declare class data members/variables and class methods.

Following is a prototype of a PHP5 class structure

class <class-name> {
	<class body :- Data Members &amp; Methods>;

On the basis of the above prototype, look below an example of PHP5 class

Example of a Class:

class Customer {
	private $first_name, $last_name;
	public function setData($first_name, $last_name) {
		$this->first_name = $first_name;
		$this->last_name = $last_name;
	public function printData() {
		echo $this->first_name . " : " . $this->last_name;

In the above program, Customer is the name of the class and $first_name/$last_name are attributes or data members. setData() and printData() are methods of a class. We will discuss more about attributes and members in the upcoming articles on PHP5 OOPS Tutorials series.

You can read more about private, public and protected in this tutorial on Access Specifiers

Naming Conventions

Naming Convention for Class:
As a general Object Oriented Programming practice, it is better to name the class as the name of the real world entity rather than giving it a fictitious name. For example, to represent a customer in your OOAD model; you should name the class as Customer instead of Person or something else.

Naming Conventions for Methods:
The same rule applies for class methods. The name of the method should tell you what action or functionality it will perform. E.g. getData() tells you that it will accept some data as input and printData() tells you that it will printData(). So ask yourself what you would name a method which stores data in the database. If you said storeDataInDB() you were right.

Look at the manner in which the method storeDataInDB() has been named. The first character of the first word of the method is lower case i.e. ‘s‘tore. For rest of the words in the function viz Data/In/DB have been named as first character Uppercase and the remaining characters of the words as lower case i.e. ‘D’ata ‘I’n ‘D’B. Therefore it becomes storeDataInDB()

Naming Convention for Attributes:
Attributes of a class should be named on the basis of the data that they hold. You should always give meaningful names to your attributes. For attributes you should split the words with an underscore (_) i.e. $first_name, $last_name, etc.

In the next tutorial we will see how to create objects in PHP5.

Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

[tags]PHP5 Tutorial, PHP Tutorial, PHP5 Class[/tags]

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:
    September 16th, 2008 at 16:32 | #1

    Can any one tell can we create a class like this

    public class Test {

    is access specifiers allowed in class declaration. If not then why PHP 5 is not allowing this?

  2. admin
    November 15th, 2008 at 20:05 | #2

    Hi Aswini,

    This is a Java class declaration.

    Since PHP5 does not have the concept of packages – this type of declaration is not possible.


  3. Inderjit
    June 17th, 2009 at 16:38 | #3

    I like your way of presenting the oops but if possible then please tell me more about the database opertions in oops

    Thanks for today

  4. sombir Jangra
    June 26th, 2009 at 12:04 | #4

    Nice. Interesting tutorial.

    Now, I am really confident with the PHP OOPS concept.

    Thank You.

  5. October 27th, 2009 at 12:01 | #5

    Hi sunil, Can We giv Condition for declaration of variables on the start of a class. For eg. If I need to call database variables seperately based on whether i’m offline or online. Like given below,

    class dbc
    private $host=”localhost”;
    private $user=”root”;
    private $password=”;
    private $database=”dboff”;
    private $host=”localhost”;
    private $user=”root”;
    private $password=’root’;
    private $database=”dbon”;
    private $conn;
    private $debug=true;
    private $log=””;


    Thanks in advance..

  6. admin
    October 27th, 2009 at 15:05 | #6

    Hi Jenson,

    It is not possible to do this… however, you can make use of Factory Pattern to achieve something similar during run time.

    Let me know if you need more info about Factory Pattern.


  7. Sathish
    November 13th, 2009 at 13:31 | #7

    Hi Sunil,

    If you provide some sample files to download it would be great, because we beginners can practice with that.

  8. December 2nd, 2009 at 17:44 | #8

    It was a very nice idea! Just want to say thank you for the information you have shared. I can see that you are putting a lot of time and effort into your blog and detailed articles. Keep posting the good work.

    Professional Business Plan

  1. November 8th, 2007 at 22:42 | #1
  2. November 8th, 2007 at 23:05 | #2
  3. November 8th, 2007 at 23:08 | #3