mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 23:06:49 +02:00
Adds late static binding to PHP reference
This commit is contained in:
@@ -693,6 +693,43 @@ use My\Namespace as SomeOtherNamespace;
|
|||||||
|
|
||||||
$cls = new SomeOtherNamespace\MyClass();
|
$cls = new SomeOtherNamespace\MyClass();
|
||||||
|
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Late Static Binding
|
||||||
|
*
|
||||||
|
* /
|
||||||
|
|
||||||
|
class ParentClass {
|
||||||
|
public static function who() {
|
||||||
|
echo "I'm a " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
public static function test() {
|
||||||
|
// self references the class the method is defined within
|
||||||
|
self::who();
|
||||||
|
// static references the class the method was invoked on
|
||||||
|
static::who();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ParentClass::test();
|
||||||
|
/*
|
||||||
|
I'm a ParentClass
|
||||||
|
I'm a ParentClass
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ChildClass extends ParentClass {
|
||||||
|
public static function who() {
|
||||||
|
echo "But I'm " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ChildClass::test();
|
||||||
|
/*
|
||||||
|
I'm a ParentClass
|
||||||
|
But I'm ChildClass
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* Error Handling
|
* Error Handling
|
||||||
*
|
*
|
||||||
@@ -708,9 +745,9 @@ try {
|
|||||||
|
|
||||||
// When using try catch blocks in a namespaced enviroment use the following
|
// When using try catch blocks in a namespaced enviroment use the following
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Do something
|
// Do something
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Handle exception
|
// Handle exception
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -719,13 +756,13 @@ try {
|
|||||||
class MyException extends Exception {}
|
class MyException extends Exception {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$condition = true;
|
$condition = true;
|
||||||
|
|
||||||
if ($condition) {
|
if ($condition) {
|
||||||
throw new MyException('Something just happend');
|
throw new MyException('Something just happend');
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (MyException $e) {
|
} catch (MyException $e) {
|
||||||
// Handle my exception
|
// Handle my exception
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user