Pages - পৃষ্ঠাসমূহ

Object Oriented Php Tutorial - some keywords

In object Oriented php we need to learn some basic keywords like static keyword, parent, final keyword , Scope Resolution Operator (::) and $this variable. This post clear about this keywords with some examlpes.
Hide Example Show Example



This tutorial contains some parts of tuorial

Object Oriented PHP - Class and object
Object Oriented PHP - Interface and abstractor
Object Oriented PHP - Constructor and destructor
Object Oriented PHP - Inheritance
Object Oriented PHP - Visivility
Object Oriented PHP - Some common keywords
some magic words in php


constant, Static, $this, parent , final keyword and Scope Resolution Operator (::)


Whats the meanings of this keywords. Short info are here.
  • static : Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
  • $this : this varibale declare the own class.
  • parent : This valiable decale the parent calss so that a child can access its parent methods.
  • final : PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

parent & $this keyword

This example show how to call parent keyword and when it use. When a class inherited another class then the child class need to access the parent function. Then ths parent keyword use for access paarent methods. And $this keyword is use for own classes variable.
<?php
 class Vehicle { 
  public $brand="Motorola";
  public $color="Black";  
  public  $model="dg12";
  
  function info() {
    echo "Model no.  ".$this->model."\n";
    echo "Brand no.  ".$this->brand."\n";
    echo "Color no.  ".$this->color."\n";
  }
 }

 class Bike extends vehicle{
    function bikeDetails() {
     public  $price="$1200";
     parent::info();
     echo "Price :   ".$this->price."\n";
    }
 }
 
 $obj = new Bike();
 
 echo $obj->bikeDetails();
?> 

Final keyword in php



The final keyword, which prevents child classes from overriding a method by defining before the method or class. Properties cannot be declared final, only classes and methods may be declared as final.
<?php
 class Bike {
    public function test() {
     echo "vehicle::test() called\n";
    }
    
    final public function moreTesting() {
     echo "vehicle::moreTesting() called\n";
    }
 }

 class car extends vehicle {
    public function moreTesting() {
     echo "car::moreTesting() called\n";
    }
 }
// Results in Fatal error: Cannot override final method Bike::moreTesting()
?>

const keyword in php

Define constant value php uses the const keyword.
<?php
 class vehicle {
  const CONST_VALUE = 'Vehicle shop';
 }
 echo vehicle::CONST_VALUE;
 
 
 $classname = 'vehicle';
 echo $classname::CONST_VALUE; // As of PHP 5.3.0
?>

Scope Resolution Operator (::)p

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing these items from outside the class definition, use the name of the class. As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). Paamayim Nekudotayim would, at first, seem like a strange choice for naming a double-colon. However, while writing the Zend Engine 0.5 (which powers PHP 3), that's what the Zend team decided to call it. It actually does mean double-colon - in Hebrew!

From outside the class
<?php
 class MyClass {
  const CONST_VALUE = 'A constant value';
 }

 $classname = 'MyClass';
 echo $classname::CONST_VALUE; // As of PHP 5.3.0

 echo MyClass::CONST_VALUE;
?>
From inside the class
<?php
 class OtherClass extends MyClass{
  public static $my_static = 'static var';

  public static function doubleColon() {
   echo parent::CONST_VALUE . "\n";
   echo self::$my_static . "\n";
  }
 }

 $classname = 'OtherClass';
 echo $classname::doubleColon(); // As of PHP 5.3.0

 OtherClass::doubleColon();
?>
when call a parent method
<?php
 class MyClass{
  protected function myFunc() {
   echo "MyClass::myFunc()\n";
  }
 }

 class OtherClass extends MyClass{
  // Override parent's definition
  public function myFunc()
  {
   // But still call the parent function
   parent::myFunc();
   echo "OtherClass::myFunc()\n";
  }
 }

 $class = new OtherClass();
 $class->myFunc();
?>

Static keyword in php

<?php
 class vehicle {
  const CONST_VALUE = 'Vehicle shop';
 }
 class car extends vehicle{
  public static $my_static = 'static var';

  public static function doubleColon() {
   echo parent::CONST_VALUE . "\n";
   echo self::$my_static . "\n";
  }
 }

 $classname = 'car';
 echo $classname::doubleColon(); // As of PHP 5.3.0

 echo car::doubleColon();
?>
Synatx for declaring static. But when we use static. Yes it is a good thing, when we not need to institiate a class to access anything.
<?php
 class vehicle {
  public static function aStaticMethod() {
   // ...
  }
 }

 vehicle::aStaticMethod();
 $classname = 'vehicleo';
 $classname::aStaticMethod(); // As of PHP 5.3.0
 ?>

See a complete example where we use static keyword, so we need not to create any instantiate of a classes.
<?php
 class Foo{
  public static $my_static = 'foo';

  public function staticValue() {
   return self::$my_static;
  }
 }

 class Bar extends Foo{
  public function fooStatic() {
   return parent::$my_static;
  }
 }


 echo Foo::$my_static . "\n";

 $foo = new Foo();
 echo $foo->staticValue() . "\n";
 echo $foo->my_static . "\n";      // Undefined "Property" my_static 

 echo $foo::$my_static . "\n";
 $classname = 'Foo';
 echo $classname::$my_static . "\n"; // As of PHP 5.3.0

 echo Bar::$my_static . "\n";
 $bar = new Bar();
 echo $bar->fooStatic() . "\n";
 ?>

No comments:

Post a Comment