×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
catch
blocktry {
undefinedFunction();
} catch (Error $e) {
echo 'Fatal error occurred: ' . $e->getMessage();
}
After running the above code on PHP 7+ the result will be:Fatal error occurred: Call to undefined function undefinedFunction()
If we run the same code on PHP 5.6, we will get the following result:<br />
<b>Fatal error</b>: Call to undefined function undefinedFunction() in <b>[...][...]</b> on line <b>5</b><br />
1. Contains at least 8 characters
2. Contains at least one uppercase letter
3. Contains at least one lowercase letter
4. Contains at least one number
5. Contains at least one special character
function age($dob) {
// Seconds in a year
$seconds = 31556926;
// Calc. age based on the provided date_of_birth val
$age = floor((time() - strtotime($dob)) / $seconds);
return $age;
}
$Datetime_object = DateTime::createFromFormat('Y-m-d', "2020-12-23");
<?
instead of <?php
then we must configure short_open_tag
setting in php.ini
to on
<?
echo '"short_open_tag" should be ON if we want to use short tags ' ;
?>
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
About abstract class:
Classes defined as abstract cannot be instantiated
Any class that contains at least one abstract method must also be abstract
Methods defined as abstract simply declare the method's signature - they cannot define the implementation
echo self::$someProperty;
Outside a classWheather::$someProperty;
self::celsiusToFarenheit(20);
Outside a classWheather::celsiusToFarenheit(20);
__destruct
gets called when we delete an object manually.
If we don't delete the object during the code execution, it will be called automatically at the end of the code executionclass User {
public $username;
public $friends = ['Tom','David'];
function __construct($name) {
$this->username = $name;
print $this->username . "'s object is created'\n";
}
function __destruct() {
print $this->username . "'s object is deleted'\n";
}
}
$user1 = new User('George');
$user2 = new User('David');
echo $user1->username . "\n";