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;
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_MESSAGEPHP 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:
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
Post a Comment