Skip to main content

OOPS in PHP part II

Final Class & Method:

method::The final keyword  allows you to mark methods so that an inheriting class can not overload them.

class myclass
final function getbaseclassname(){
return _class_;
}

After declaring a class as final ,it can't inherited.

The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.

Class Constants :

Class constants can be useful if you need to define some constant data within a class.
A class constant is declared inside a class with the const keyword.
We can access a constant from outside the class by scope resolution operator(::).
class Goodbye {
  const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}

echo Goodbye::LEAVING_MESSAGE;
Or, we can access a constant from inside the class by using the self keyword followeed by resolution, like self::LEAVING_MESSAGE

PHP 8.1 Final Class Constants: 
 
 PHP 8.1 supports the final flag on class constants.
the final flag was not allowed on class constants, which would have protected class constants from being overridden by sub classes. Attempting to use the final flag on class constants resulted in a fatal error.
 

Overloading:

Function overloading or method overloading is a feature that allows creating several methods with the same name which differ from each other in the type of the input parameters. It is simply defined as the ability of one function to perform different tasks.

For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. It is a form of static polymorphism which .The decision to call an implementation or another is taken at coding time.

Function overriding

Function overriding occurs when you extend a class and rewrite a function which existed in the parent class:

Traits:

One of the problems of PHP as a programming language is the fact that you can only have single inheritance.

To overcome this proublem traits introduces in php 5.4 and onwords,

A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own.


 trait Sharable {  
   public function share($item){   
   return ‘share this item’;   
   }  
 }  
 //use it on your own class like below ,  
 class Post {  
  use Sharable;  
 }  
 class Comment {  
  use Sharable;  
 }   

We can use multiple traits at the same time, 
like With in class,

class {

use Sharable,traits2,traits3;

}

A Trait is basically just a way to “copy and paste” code during run time.

Method Overriding in Traits: 
In inheritance We can override the function with in child class , That is the feature of inheritance. But if you are using traits, then you can not override the function.
The order of using the function is , 
First it will check the function abc in child class  if exists it would be used, if not there then check in traits if found then traits function would be used, if not available in traits then it fill find in base class,
So precedence would be : child class -> traits -> base class
 class Abc {  
  public function abc(){  
  echo "I am From Abc Class";   
  }  
 }  
 traits test{  
  public function abc(){  
  echo "I am From Traits test";  
  }  
 }  
 Class xyz extends Abc{  
  use test;  
  public function abc(){  
  echo "I am From Xyz Class";  
  }  
 }  
 $d = new xyz();  
 $d-abc();  

Late Static Binding : 

Compile-time and run-time, When Code compile it checks all the code and checks errors if exists if all is good then go running.
We can access the static variable by self keyword which is in the same class only,

Now Talk about the late static Binding means assign the value at the run time instead of compile time.
As given example, When it compiles it and finds the static::$variable name then,, the compiler left them blank to assign the value at run time.

 
 class DB{  
  protected static $table = 'base_table';  
  public function select(){  
  echo "Select * from".static::$table;   
  }  
  public function insert(){  
  echo "INSERT INTO".static::$table;   
  }  
 }  
 class UserAccounts extends DB{  
  protected static $table = 'users';  
 }  
 $accounts = new UserAcconts();  
 $accounts->select();  
 //output would be   
 select * from users;  


When we create an instance of the class then it will catch the variable of the derived class not from the base class. In Modern frameworks like CakePHP, YII also uses the same concept to write the models.


Static Members , function & class

Members(Variables) : We can access the variables by $this,self & static keyword
$this is used within the class when creating the instance of the class.

Static variables are related to the class not the instance of the class.
class Abc{
public static $name = 'Aman';
}
echo Abc::name; //aman

So we can access the variables and methods without creating the instance of the class by static Keyword.

Comments

Popular posts from this blog

How to span column of custom table in Drupal

If you want to span the column of custom drupal table like below image, Follow the below code to make the header of the table , <?php $header = array('S.N','District', array('data' => '<div class ="house">Household </div><br><span>Rural</span> <p>Urban</p>','colspan' => 2), array('data' => '<div class ="house">Members</div> <br><span>Rural</span> <p>Urban</p>','colspan' => 2), 'Duplicate/Failed Registration', array('data' => '<div class ="house">Pending De duplication </div><br><span>Rural</span> <p>Urban</p>','colspan' => 2), 'Non Un-organised Workers', 'SSID Generated', 'No. of Card Personlised', ); $rows[] = arra...

Drupal 8 : Link actions,Link menus,Link Tasks,Routings

Drupal 8 : Link actions,Link menus,Link Tasks,Routings Link actions Local actions have also been moved out of the hook_menu() system in Drupal 8 .Use actions to define local operations such as adding new items to an administrative list (menus, contact categories, etc). Local actions are defined in a YAML format, named after the module they are defined by. Such as menu_ui.links.action.yml for this example from menu_ui module: menu_ui.link_add:   route_name: menu_ui.link_add   title: 'Add link'   appears_on:     - menu_ui.menu_edit Here, menu_ui.link_add: It is the Unique name of the link action Most likely start with module name, route_name : Name of the route it means when click the link it redirect to this route, appears_on :  An array of route names for this action to be display on. Now how to know the Route name of any internal/external admin pages like below, By through the drupal console we achieve it, drupal debug:router...

Cache In Drupal

Drupal 8 core caching modules: The Internal Page Cache module: this caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused for future visitors. The Internal Dynamic Page Cache module: This is a key feature that Drupal 7 did not have. Unlike the Internal Page module, the Dynamic Page module aims to speed up the site for both anonymous and logged-in users. How Cache Works in Drupal: There are two modules available in the drupal core 1 - Internal page cache: The Internal Page Cache module caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused. Configuring the internal page cache :   From the performance page we can set the maximum time how long browsers and proxies may cache pages based on the Cache-Control header. 2 - Internal dynamic page cache: Drupal 8 provides the Dynamic Page Cache m...