Created
June 28, 2021 06:37
-
-
Save MaraScott/5573f59113c0a9e9881c8c7dd4450490 to your computer and use it in GitHub Desktop.
Php abstract class, interface, trait, final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "ABSTRACT : DECLARE CLASS THE WAY YOU WANT", "\n\n"; | |
abstract class AbstractClass | |
{ | |
// Our abstract method only needs to define the required parameters | |
abstract protected function prefixName($name); | |
} | |
class ConcreteClass extends AbstractClass | |
{ | |
// Our subclass can define optional parameters that do not exist in the parent signature | |
public function prefixName($name, $separator = ".") { | |
if ($name == "Pacman") { | |
$prefix = "Mr"; | |
} elseif ($name == "Pacwoman") { | |
$prefix = "Mrs"; | |
} else { | |
$prefix = ""; | |
} | |
return "{$prefix}{$separator} {$name}"; | |
} | |
} | |
$class = new ConcreteClass; | |
echo $class->prefixName("Pacman"), "\n"; | |
echo $class->prefixName("Pacwoman"), "\n\n"; | |
echo "--------------------------------", "\n"; | |
echo "INTERFACE : SAME AS ABSTRACT BUT PUBLICLY", "\n"; | |
echo "An interface is a special abstract class that can also be thought of as a specification for a model", "\n\n"; | |
// Define the interface | |
interface User{ | |
function getDiscount(); | |
function getUserType(); | |
} | |
//VIP user interface implementation | |
class VipUser implements User{ | |
// VIP user discount factor | |
private $discount = 0.8; | |
function getDiscount() { | |
return $this->discount; | |
} | |
function getUserType() { | |
Return "VIP user"; | |
} | |
} | |
class Goods { | |
var $price = 100; | |
var $vc; | |
// Define the User interface type parameter, then do not know what user | |
function run(User $vc){ | |
$this->vc = $vc; | |
$discount = $this->vc->getDiscount(); | |
$usertype = $this->vc->getUserType(); | |
Echo $usertype."Commodity Price:".$this->price*$discount; | |
} | |
} | |
$display = new Goods(); | |
$display ->run(new VipUser); //can be more other user types | |
echo "\n\n"; | |
echo "--------------------------------", "\n"; | |
echo "TRAIT : ADD METHOD WITHOUT BEING A PARENT", "\n\n"; | |
trait Hello { | |
public function sayHello() { | |
echo 'Hello '; | |
} | |
} | |
trait World { | |
public function sayWorld() { | |
echo 'World'; | |
} | |
} | |
class MyHelloWorld { | |
use Hello, World; | |
public function sayExclamationMark() { | |
echo '!'; | |
} | |
} | |
$o = new MyHelloWorld(); | |
$o->sayHello(); | |
$o->sayWorld(); | |
$o->sayExclamationMark(); | |
# Hello World! | |
echo "\n\n"; | |
echo "--------------------------------", "\n"; | |
echo "FINAL : METHOD THAT CAN'T BE INHERITED/OVERRIDED", "\n\n"; | |
class BaseClass { | |
public function test() { | |
echo "BaseClass::test() called\n"; | |
} | |
final public function moreTesting() { | |
echo "BaseClass::moreTesting() called\n"; | |
} | |
} | |
class ChildClass extends BaseClass { | |
// Results in Fatal error: Cannot override final method BaseClass::moreTesting() | |
/* | |
public function moreTesting() { | |
echo "ChildClass::moreTesting() called\n"; | |
} | |
*/ | |
} | |
final class BaseClass2 { | |
public function test() { | |
echo "BaseClass::test() called\n"; | |
} | |
// No matter whether you declare the method as final or not, it doesn't matter. | |
final public function moreTesting() { | |
echo "BaseClass::moreTesting() called\n"; | |
} | |
} | |
// Generate Fatal error: Class ChildClass may not inherit from final class (BaseClass) | |
/* | |
class ChildClass extends BaseClass2 { | |
} | |
*/ | |
// src : https://www.programmersought.com/article/79471399333/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment