Skip to main content

Posts

Showing posts with the label PHP

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 F...

What's New in PHP 7

The following are some of the new features PHP 7 1 - Scalar type declarations : if you use the declare(strict_types=1); then the parameter would be strict, i.e folat means float only if you pass anything else it will through the error, declare(strict_types=1); function returnsum(float $a, float $b)  { return $a + $b; } returnsum(3.1, 2.1); // output float(5.2) returnsum(3, "2 days"); // Fatal error 2 - Return type declaration : same as scaler type declaration, we can use the return with in a function with strictly. declare(strict_types=1); function add($a, $b): int { return $a + $b; } var_dump(add(1, 2)); var_dump(add(1, 2.5)); // output will be // int(3) // Fatal error 3 - Null Coalescing Operator : if the value is exists and not null, then it returns the first operand, otherwise it returns the second operand. $username = $_GET['username'] ?? 'not define'; // Fetches the value of $_GET['username'] and returns 'not def...

OOPS in PHP part I

Static Methods and Properties: Static Methods and Properties is very useful feature in PHP and in this post i am going to tell you about Static Methods and Properties in PHP with example. If you declare any class property or methods as static then you don't need to create object of class to access that means it can be accessible without creating object of class. You can access directly from class with the help of scope resolution operator (::). You can use  static  keyword to define static methods and properties. class myClass { static public $variable1 = 5; static public $variable2 = 2; static public function getSum() { $sum=self::$variable1+self::$variable2; print "Sum of two variables is " . $sum; } } echo myClass::$variable1; myClass::getSum(); If you extends the parent class in child class and want to access parent class property then you can use parent keyword. parent::$variable1 Why use static methods and proper...