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,
Public
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>";
- }
-
- }
-
-
-
- $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.
- <?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
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();
- ?>
Abstract Class
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.
- <?php
- abstract class employee
- {
- protected $empname;
- protected $empage;
-
- function setdata($empname,$empage)
- {
- $this->empname = $empname;
- $this->empage = $empage;
- }
-
- abstract function outputData();
- }
-
- class EmployeeData extends employee
- {
- function __construct($name,$age)
- {
- $this->setdata($name,$age);
- }
-
- function outputData()
- {
- echo $this->empname;
- echo $this->empage;
- }
- }
-
- $a = new EmployeeData("Hitesh","24");
- $a->outputData();
- ?>
Interface Class
Interface class doesn’t add any additional functionality but it outlines a standard format which your classes need to use. Any class that inherits from Interface Class must have the same methods as the interface class.
Interface classes are defined by using the ‘Interface’ Keyword. The nature of inheritance is always public; i.e, all the methods In the interface must be public.
Syntax
- interface iTemplate
- {
- public function setVariable($name, $var);
- public function getHtml($template);
- }
- class Template implements iTemplate
- {
- private $vars = array();
-
- public function setVariable($name, $var)
- {
- $this->vars[$name] = $var;
- }
-
- public function getHtml($template)
- {
- foreach($this->vars as $name => $value) {
- $template = str_replace('{' . $name . '}', $value, $template);
- }
-
- return $template;
- }
- }
-
-
-
-
-
-
-
- class BadTemplate implements iTemplate
- {
- private $vars = array();
-
- public function setVariable($name, $var)
- {
- $this->vars[$name] = $var;
- }
- }
- ?>
Final Class
Final Class can’t be extended. A final keyword is used before the class to which you want to declare Final.
- final class BaseClass {
- public function myMethod() {
- echo "BaseClass method called";
- }
- }
-
-
- class DerivedClass extends BaseClass {
- public function myMethod() {
- echo "DerivedClass method called";
- }
- }
-
- $c = new DerivedClass();
- $c->myMethod();
Here in the above code snippet we are seeing that baseclass is declared as final and thus it can’t be extended (inherited). DerivedClass tries to extend from BaseClass, therefore the compiler will throw a compile error.