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.
- Private - By declaring method as private, these methods can only be accessed within the class. So outside world cannot access this method
- Protected - By declaring method as protected, these methods can only be accessed by the derived class while performing Inheritance Operation but the outside world has no access to these members
- Public - By declaring method as public, these methods can be accessed internally and by outside world also
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
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