Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - Learn to create a PHP5 Class

Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

11

If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!

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.

Note:
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

Your email:

 

Technorati Tags: , ,

Comments (11)

[...] my DNA is made up of 1s and 0s « PHP5 OOPS Tutorial - Magic Methods - __clone() method PHP5 OOPS Tutorial - Learn to create a PHP5 Class [...]

[...] PHP5 OOPS Tutorial - Learn to create a PHP5 Class [...]

[...] PHP5 OOPS Tutorial - Learn to create a PHP5 Class [...]

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?

Hi Aswini,

This is a Java class declaration.

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

Regards,
Suniil

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

Nice. Interesting tutorial.

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

Thank You.

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
{
if(strtolower($_SERVER['SERVER_NAME'])==’localhost’)
{
private $host=”localhost”;
private $user=”root”;
private $password=”;
private $database=”dboff”;
}
else
{
private $host=”localhost”;
private $user=”root”;
private $password=’root’;
private $database=”dbon”;
}
private $conn;
private $debug=true;
private $log=”";

}

Thanks in advance..

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.

Regards,
Suniil

Hi Sunil,

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

Hi,
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

Write a comment

Enter this code