Today we will learn OOPS which is a very important concept used in any program. Every language like PHP,.NET, JAVA ,etc. uses OOPS concepts. OOPS stands for object oriented programming structure. OOPS depends on class and object. Properties use ‘access modifiers’ in oops.
There are 3 access modifiers,
There are 3 access modifiers,
- public
- private
- protected
Public
Public classes are those classes which can refer to field or method. No restriction is there for any individual to access.
Syntax
Public classes are those classes which can refer to field or method. No restriction is there for any individual to access.
Syntax
- <?php
- class Member {
- public $username = "";
- private $loggedIn = false;
- public function login() {
- $this->loggedIn = true;
- }
- public function logout() {
- $this->loggedIn = false;
- }
- public function isLoggedIn() {
- return $this->loggedIn;
- }
- }
- class Administrator extends Member {
- public function createForum( $forumName ) {
- echo "$this->username created a new forum: $forumName<br>";
- }
- public function banMember( $member ) {
- echo "$this->username banned the member: $member->username<br>";
- }
- }
- // Create a new member and log them in
- $member = new Member();
- $member->username = "Fred";
- $member->login();
- echo $member->username . " is " . ( $member->isLoggedIn() ? "logged in" : "logged out" ) . "<br>";
- ?>
Private
Private classes are those classes in which only the current class has acess to its fields and methods.
Private classes are those classes in which only the current class has acess to its fields and methods.
- <?php
- class WebDeveloper {
- private $name;
- private function __construct($name) {
- $this->name = $name
- }
- }
- $developer = new WebDeveloper('jordizle');
- echo $developer->name;
- ?>
The above program will show an error because the attribute is set to private. To access the attributes the class should have a public method that can return the value.
- <?php
- class WebDeveloper {
- private $name;
- private function __construct($name) {
- $this->name = $name
- }
- public function getName() {
- return $this->name;
- }
- }
- $developer = new WebDeveloper('jordizle');
- echo $developer->getName();
- ?>
Protected classes are those classes in which only the current class and subclass of this class would have access to the field or method.
- <?php
- class dog {
- public $Name;
- protected function getName() {
- return $this->Name;
- }
- }
- class poodle extends dog {
- public function bark() {
- print "'Woof', says " . $this->getName();
- }
- }
- $poppy = new poodle;
- $poppy->Name = "Poppy";
- $poppy->bark();
- ?>
An abstract class is a class that contains at least one abstract method.This is partially implemented by the programmer. An abstract method is just a function definition that tells the programmer that the method must be implemented in the child class. It can contain one or more abstract methods.
An abstract class is created by using abstract keyword. If we have made any class abstract then we cannot further create any object in that class. If a function is defined as private then it cannot be declared as private. It can only be declared as public and protected.

SubashPosted Aug 2, 2016, 1:44 AM
Thanks for Sharing