OOPS in PHP 5 - Define Methods for the Class

Now once you have declared class and all its data members now will have to declare all the methods that the class will use. This tutorial will guide you to create methods for class.
What are Methods in Class?
Methods defines the functionality for the class. Methods acts on the data and communicate with each other by means of message passing. Methods can be declared as Private, Protected or Public Access Specifiers.




Example:

< ?
class Customer
{
   public $name;
 
   public function getName()
   {
      return $this->name;
   }
   public function displayName()
   {
      return $this->getName();  //Access Method within the class    
   }
}
$a = new Customer();
$a->displayName();    //Access Method outside the class    
?>

Here you see that i have declared method getName() and displayName() for the class Customer. Now to access the methods within the class $this operator is used. To access the method from outside the class the object variable is used to access it.

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)