Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial – Defining Attributes of a PHP5 Class

PHP5 Tutorial – Defining Attributes of a PHP5 Class

November 8th, 2007 admin Leave a comment Go to comments

In this tutorial you will learn about class attributes and how to declare & use them in PHP5 classes.

Definition of an class attribute
An attribute is also know as data members and is used to hold data of a class. The data that it holds are specific to the nature of the class in which it has been defined. For example, a Customer class would hold data related to a customer, an Order class would hold data related a an order.





Defining an attribute is as easy as declaring a variable within the body of the class. At the time of declaring a data member/variable within the body of the class, it is also possible to assign a default value to the attribute or data member.

Attributes can either be public, private or protected – the default being public. These are called Access Specifiers. You will learn more about access specifiers in PHP5 OOPS tutorials ahead.

Example Code:

class Customer {
	private $first_name, $last_name;
	private $outstanding_amount = 0; //example of default value
 
	public function getData($first_name, $last_name) {
		$this->first_name = $first_name;
		$this->last_name = $last_name;
	}
 
	public function printData() {
		echo $this->first_name . " : " . $this->last_name;
	}
}
 
$c1 = new Customer();

In the above example $first_name, $last_name and $outstanding_amount are data members or attributes of the class Customer. Of these attributes, $outstanding_amount has a default value of zero assigned to it. This means that when an object of the Customer class has been created, $first_name and $last_name will be blank whereas; $outstanding_amount will be initialized with default value of zero.

Let’s take a problem statement and define a PHP5 Class for it.

Problem Statement:
Define a class to encapsulate Student. The data required to be captured is Student’s first name, last name, date of birth, address and telephone number.

Solution:

class Student {
	public $first_name;
	public $last_name;
	public $date_of_birth;
	public $address;
	public $telephone;
}

In the above example $first_name, $last_name, $date_of_birth, $address and $telephone are all data members of class Student. These data members define the data that one Student Object will store. Therefore, you can create two objects of a Student class having different data as follows:

//creating an object $s1 of Student class and assigning data
$s1 = new Student();
$s1->first_name = "Sunil";
$s1->last_name = "Bhatia";
$s1->date_of_birth = "01/01/1900";
$s1->address = "India";
$s1->telephone = "9999999999";
 
//creating an object $s2 of Student class and assigning data
$s2 = new Student();
$s2->first_name = "Vishal";
$s2->last_name = "Thakur";
$s2->date_of_birth = "01/01/1910";
$s2->address = "USA";
$s2->telephone = "8888888888";

In the above example, we create two objects of the Student class $s1 and $s2. Both these objects have their own memory space within which they store their respective data. Because each data member is ‘public‘ you can directly access the data members of the Student Class using the arrow operator (reference operator) i.e. $s1->first_name = “Sunil”; You will learn more about public, private and protected in the PHP5 tutorials ahead on Access Specifiers.

The diagram below explains the above example:

PHP5 Tutorial - Example of defining attributes of a PHP5 Class




Objects as Attributes

In addition to declaring attributes as intrinsic data types (int, string, etc), you can also declare data members as objects of another class. This is called aggregation in Object Oriented Analysis and Design (OOAD). Lets look at an example below:

class Customer {
	private $first_name, $last_name;
	private $outstanding_amount = 0; //example of default value
 
	public function getData($first_name, $last_name) {
		$this->first_name = $first_name;
		$this->last_name = $last_name;
	}
 
	public function printData() {
		echo $this->first_name . " : " . $this->last_name;
	}
}
 
class Order {
	private $order_id;
	private $customer;
 
	public setCust(Customer $c) {
		$this->customer = $c;
	}
}
 
$c1 = new Customer();
$o1 = new Order();
 
$o1->setCust($c1); //storing $c1 object in $o1 order object

In the above example setCust() method accepts a Customer type of parameter which is stored internally in the $customer data member.

The advantage of the above method is that it allows you to change the customer object independently of the order object. Imagine having to add 3 – 4 new data members to the Customer object. You only have to modify the Customer object without having the need to modify the Order Object.

In the next tutorial you will learn about PHP5 Class Method.

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

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Yukti
    September 8th, 2008 at 16:52 | #1

    In the above example setCust() method accepts a Customer type of parameter

    What is “Customer type” of parameter. ?

    2) What if I pass 11 :
    $o1->setCust(11);

    3) Why do I need to pass the object :
    $c1 = new Customer();
    $o1->setCust($c1);

    when I already have

    public setCust(Customer $c)
    {
    $this->customer = $c;
    }

    “setCust(Customer $c ) ” here ?

  2. admin
    November 15th, 2008 at 19:49 | #2

    Hi Yukti,

    With this:

    What if I pass 11 :
    $o1->setCust(11);

    You are wanting to assign number 11 as the customer object. This is not possible as 11 is an integer and the method setCust expects a Customer type of object.

    The above example only shows you how you can pass objects as parameters to your methods.

    Let me know if you need anything specific to understand PHP5 attributes.

    Regards,
    Suniil

  3. Bidyut Kumar
    April 17th, 2009 at 03:12 | #3

    hi its a awesome to have all the Php class concept on one platform

  4. Bidyut Kumar
    April 17th, 2009 at 03:13 | #4

    hi it was nice experience

  5. September 22nd, 2009 at 20:24 | #5

    Thank you for the tutorial. It’s helpful. I had trouble getting the last example to work. But I figured out it was missing some structure…


    public setCust(Customer $c) {

    should be…


    public function setCust(Customer $c) {

    the”function” word was left out.

  6. Hina
    November 19th, 2009 at 15:13 | #6

    Hi Sunil!

    I am referring your site http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class#more-59 for “Object Aggregation in Object and Attributes section” please refer the example Customer and Order.

    I have understood this concept very well but not being able to understand what do you mean by “We can create the 3 – 4 new data members to the Customer object. You only have to modify the Customer object without having the need to modify the Order Object. ”

    Please explain. Thanks in advance.

    Thanks,
    Hina

  7. admin
    November 21st, 2009 at 10:36 | #7

    Hi Hina,

    By this I mean that new data members can be added e.g. address, email, etc… without changing the method signature of Order->setCust().

    In non OO languages, you either had to pass on a struct or pass all the parameters.

    A OO language allows you to keep adding on to class members without having to modify methods which accept it as parameters.

    Regards,
    Suniil

  8. December 10th, 2009 at 17:26 | #8

    this is good tutorial for object oriented in php5

  1. November 8th, 2007 at 20:11 | #1
  2. November 8th, 2007 at 22:52 | #2
  3. November 8th, 2007 at 22:57 | #3
Enter this code to leave comment (Sorry, but bots get me crazy :) )