PHP5 Tutorial - Defining Methods of a PHP5 Class
Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials
8
In this PHP5 tutorial you will learn about methods and how to declare and use them in PHP5 class.
Definition of an class method
A class method/functions is the behavior/functionality of a class i.e. they provide the necessary code for the class in which it is defined. Examples could be a saveCustomer() method in the class Customer or a printDocument() in the Document class.
Methods act (perform operations) on the data members of the class and can be declared as private or public. A class method is exactly similar to PHP functions, it’s just that class functions are declared inside classes and accessed using the -> (arrow operator / dereferencing operator).
Methods can also be declared as either public, protected or private.
Lets look at an example to understand PHP5 Class methods better:
Example Code:
class Customer { private $name; public functionsetName($name) { $this->name = $name; } } $c1 = new Customer(); $c1->setName("Sunil Bhatia");
In the above example setName() is the class method of the Customer class. The setName() class method is responsible for accepting the name of the customer and storing it in the internal data member i.e. $name.
The reason why you require methods is so that you can perform necessary validations on the data passed. Let’s re-look at the above example with necessary validation code.
class Customer { private $name; public function setName($name) { if(trim($name) != "") { $this->name = $name; return true; } else { return false; } } } $c1 = new Customer(); $c1->setName("Sunil Bhatia");
In the above example the setName() method accepts a customer’s name and validates to check if $name is blank. If $name is blank the setName() function returns false; otherwise it stores the $name in the $this->name of the class and returns true.
Notes on Accessor and Mutator methods
Although we will learn about Access Specifiers in the subsequent tutorials; let’s try to understand the meaning and need for accessor and mutator methods.
Accessor Methods:
Accessor methods are also know as getter methods. The reason why we need an accessor method is to be able to read the value of a property/attribute in a class object. In real OOAD practice most of the data members that you define would either be private or protected (more on this will be covered in the tutorial on Access specifiers), therefore to access data of such data members that have been defined as either private or protected will require an implementation of accessor or getter methods.
Note: To make a property or data member as non-read only; you should not provide a getter or accessor method.
Mutator Methods:
Mutator methods are opposite to accessor methods. Mutator methods provides a mechanism to store data in data members that have either been declared as private or protected. The reason why you should provide a mutator method is to provide necessary validation on the data that is to be stored in the data member of the class.
Note: To make a property or data member as read only; you should not provide a setter or mutator method.
Lets look at an example of accessor and mutator methods below:
class Customer { private $name; //mutator method public function setName($name) { if(trim($name) != "") { $this->name = $name; return true; } else { return false; } } //accessor method public getName() { return $this->name; } } $c1 = new Customer(); $c1->setName("Sunil Bhatia"); echo $c1->getName();
Output:
Sunil Bhatia
In the above example the setName() method accepts a customer’s name and validates to check if $name is blank. If $name is blank the setName() function returns false; otherwise it stores the $name in the $this->name of the class and returns true. The getName() returns the name stored in the $name data member of the $c1 object.
In the next tutorial you will learn about PHP5 Constructor.
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)
- PHP5 Tutorial - Learn to create a PHP5 Class
- PHP5 Tutorial - Learn to Create a PHP5 Class Object
- PHP5 Tutorial - Defining Attributes of a PHP5 Class
- PHP5 Tutorial - Defining Methods of a PHP5 Class
- PHP5 Tutorial - Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
- PHP5 Tutorial - Magic Methods - __toString() method
- PHP5 Tutorial - Magic Methods - __get() and __set()
- PHP5 Tutorial - Magic Methods - __isset() and __unset()
- PHP5 Tutorial - Magic Methods - __call() method
- PHP5 Tutorial - Magic Methods - __autoload() method
- PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
- PHP5 Tutorial - Magic Methods - __clone() method









[...] PHP5 OOPS Tutorial - Defining Methods of a PHP5 Class [...]
Hi
i wanted to ask a question about mutator method, you defined it twice on the example, im confused.. whats the different between them, they look the same.
i thought first one would be because of validation and then once it returns true then it will call it again and next one will insert the data. but your example as both things same, not a difference between them. can you explain more bit on them please.
Best Regards
Basit
Hi Basit,
My apologies, there was a mistake in the code. I have corrected the same. I had copied the mutator method twice.
Sunil
Hi, first of all I want to thank you for the brilliant tutorials.
I am just curious…
Shouldn’t there be “function” exclusively mentioned on this line
public setName($name)
like Public function setname($name)
Sorry Im very new to PHP, when I copied and paste the above
codes to my php editor and run the script it comes with error.
Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE in C:\Apache2\htdocs\Testing\OOP_Attributes.php on line 6
So I thought I try by exclusively mentioning “function” and
it worked.
Nice. Interesting tutorial.
Now, I am really confident with the PHP OOPS concept.
Thank You.
Hello,
This is a nice website.
In the example related to class I feel the keyword ‘function’ will be added before the function name. As for example it should be
public function setName()
instead of
public setName()
Please ask your queries. Please let me know whether I am right or wrong.
Regards.
Hi Amit,
Thank you for pointing out the mistake. I have fixed the same.
Regards,
Suniil
Took me hours to work out why your code did not work.
Can you change the page so that other people don’t have the same problems?
Apart from that its a cool lession.