private $name, $lastname; public function __construct($name, $lastname) { $this->name = $name; $this->lastname = $lastname; } public function GetFullName() { return $this->name . " " . $this->lastname; } } class ChildClass extends ParentClass { private $age; public function __construct($name, $lastname, $age) { parent::__construct($name, $lastname); $this->age = $age; } public function GetAge() { return $this->age; } public function asd() { return $this->GetFullName(); } } $obj = new ChildClass("Pedro", "Sanchez", 30); echo "<p>Nombre completo: <b>{$obj->asd()}</b></p>"; echo "<p>Edad: <b>{$obj->GetAge()}</b></p>";
print method_exists("Emailer", "addRecipients"); print "</br>"; print property_exists("Emailer", "test"); print "</br>"; /*---------- Checking the Type of a class ----------*/ $cc = new ChildClass(); if (is_a($cc, "ChildClass")) { print "It's a ChildClass Type Object. </br>"; } if (is_a($cc, "ParentClass")) { print "It's also a ParentClass Type Object. </br>"; } /*---------- Finding out the Class Name ----------*/ print get_class($cc); print "</br>"; $cc2 = new ChildClass(); print $cc2->getClass(); print "</br>"; /*---------- Exception Handling ----------*/ /*$db = new DB(); if(!$db->connect()) print("Failed to connect to PostgreSQL Server"); print("</br>");*/ $dbexc = new DB(); try { $dbexc->connect(); } catch (Exception $e) { print "<pre>"; var_dump($e); print "</pre>"; } print "</br>";
public function __construct($name, $parent = null) { $this->name = $name; $this->parent = $parent; } public function getParent() { return $this->parent; } } class ChildClass extends ParentClass { private $parent; public function getParent() { return $this->parent; } public function setParent($parent) { $this->parent = $parent; } } $parent1 = new ParentClass('parent1'); $parent2 = new ParentClass('parent2'); $child = new ChildClass('child1', $parent2); $child->setParent($parent2); // echo $child->getParent()->name; $property = new ReflectionProperty('ParentClass', 'parent'); $property->setAccessible(true); $property->setValue($child, $parent1); assert('parent2' == $child->getParent()->name, 'parent value of ParentClass should be changed, not of ChildClass');
<?php class TestClass { public static function getClassName() { return get_called_class(); } } class ChildClass extends TestClass { } echo TestClass::getClassName() . "\n"; echo ChildClass::getClassName() . "\n"; ?> ==DONE==
} private function privateMethod() { return 'FatherClass::privateMethod()'; } protected function protectedMethod() { return 'FatherClass::protectedMethod()'; } public function printPrivateMethod() { return $this->privateMethod(); } } class ChildClass extends FatherClass { public function printProtectedMethod() { return $this->protectedMethod(); } } $oFatherClass = new FatherClass(); $oChildClass = new ChildClass(); var_dump($oChildClass->publicMethod()); var_dump($oFatherClass->publicMethod()); //var_dump($oChildClass->privateMethod()); //PHP Fatal error: Call to private method FatherClass::privateMethod() //var_dump($oFatherClass->privateMethod()); //PHP Fatal error: Call to private method FatherClass::privateMethod() var_dump($oFatherClass->printPrivateMethod()); //var_dump($oChildClass->protectedMethod()); //PHP Fatal error: Call to protected method FatherClass::protectedMethod() //var_dump($oFatherClass->protectedMethod()); //PHP Fatal error: Call to protected method FatherClass::protectedMethod() var_dump($oChildClass->printProtectedMethod());
public static function test() { $myChild = new ChildClass(); $myChild->secret(); // bug - invokes X::secret() instead of ChildClass::secret() }
<?php class BaseClass { private $private_base = "Base"; function printVars() { var_dump($this->private_base); var_dump($this->private_child); } } class ChildClass extends BaseClass { private $private_child = "Child"; } echo "===BASE===\n"; $obj = new BaseClass(); $obj->printVars(); echo "===CHILD===\n"; $obj = new ChildClass(); $obj->printVars(); ?> ===DONE===
public static function &GetCurrent() { return ChildClass::Get(); }
<?php class BaseClass { var $class_name = "BaseClass"; function BaseClass($value) { print "value is '{$value}'\n"; } function MyClassName() { return $this->class_name; } } class ChildClass { var $class_name = "ChildClass"; function ChildClass($value, $new_value) { BaseClass::BaseClass($value); print "new value is '{$new_value}'\n"; } function MyClassName($a_value) { return BaseClass::MyClassName() . " and the value is '{$a_value}'"; } } $obj = new ChildClass("Test", "Another test"); print $obj->MyClassName("not interesting");
public function fun_with_args(ChildClass $class, $whatever = null) { return $class->fun_name($whatever); }
* 子类中编写跟父类完全一致的方法名 可以完成 对父类方法的重写 * 添加final关键字能让这个方法不能被子类重写 但是可以调用 final public function(); * 对不不想被任何类继承的类 可以在类前面 添加 final, final class Class(); //禁止重载方法 */ final class BaseClass { //添加final关键字能让这个方法不能被子类重写 public final function test() { echo "BasicClass::test1"; } public function test1() { echo "BasicClass::test1"; } } class ChildClass extends BaseClass { public function test($tmp = null) { echo "childClass::sss"; } } $obj1 = new ChildClass(); $obj1->test('tmp');
public static function test() { ChildClass::getProtected(); ChildClass::getPrivate(); }
<?php class ParentClass { private $prVar = "I'm private variable from ParentClass!"; protected $prVar2 = "I'm private variable2 from ParentClass!"; public function myFunctionA() { echo "public function from ParentClass<br>"; } protected function myFunctionB() { return $this->prVar; } } class ChildClass extends ParentClass { public function myFunctionC() { //MISTAKE!!! return "USING parent value1:" . $this->prVar . " <br>" . "USING parent value2:" . $this->prVar2; } } $child = new ChildClass(); //ERROR! //$child->myFunctionB(); echo "<hr>"; $child->myFunctionA(); echo "<hr>"; //ERROR! echo $child->myFunctionC();
<?php date_default_timezone_set("PRC"); /** * 重写和Final * 1. 子类中编写跟父类完全一致的方法可以完成对父类方法的重写 * 2. 对于不想被任何类继承的类可以在class之前添加final关键字 * 3. 对于不想被子类重写(overwrite, 修改)的方法,可以在方法定义前面添加final关键字 */ class BaseClass { public function test() { echo "BaseClass::test() called\n"; } public function moreTesting() { echo "BaseClass::moreTesting() called\n"; } } class ChildClass extends BaseClass { // 重写时参数不一定要跟父类完全一致 public function moreTesting($tmp = null) { echo "ChildClass::moreTesting() called\n"; } } // Results in Fatal error: Cannot override final method BaseClass::moreTesting() $obj = new ChildClass(); $obj->moreTesting();
<?php require_once 'iExample-interface.php'; require_once 'iExampleTwo-interface.php'; require_once 'ChildClass-class.php'; $childClass = new ChildClass(); $childClass->setWage(1000); $childClass->setTaxRate(6); $messages; $messages[] = 'Het loon bedraagt ' . $childClass->getWage() . '€'; $messages[] = 'De belastingsprocent bedraagt ' . $childClass->getTaxRate() . '%'; require_once 'voorbeeld-classes-interface-view.php';
<?php class ParentClass { } class ChildClass extends ParentClass { public function testIsCallable() { var_dump(is_callable(array($this, 'parent::testIsCallable'))); } public function testIsCallable2() { var_dump(is_callable(array($this, 'static::testIsCallable2'))); } } $child = new ChildClass(); $child->testIsCallable(); $child->testIsCallable2();
*/ class BaseClass { public function test() { echo "BasicClass::test1"; } public function test1() { echo "BasicClass::test1"; } } class ChildClass extends BaseClass { const CONST_VALUE = "a const value"; private static $sValue1 = "static value <br/>"; public function test($tmp = null) { echo "childClass::sss"; parent::test(); self::called(); echo self::CONST_VALUE; echo static::$sValue1; } public function called() { echo "this is a function called<br/>"; } } $obj1 = new ChildClass(); $obj1->test();
protected abstract function youNeedToProtectThisMan(); } class ChildClass extends AbstractClass { public function youNeedThisMan() { echo 'I got this bro!'; } protected function youNeedToProtectThisMan() { echo 'I got this protected bro!'; } public function printProp() { echo $this->someProp; } } $class = new ChildClass(); $class->youNeedThisMan(); echo '<br />'; $class->printProp(); $class->youNeedToProtectThisMan(); // Will error out /* Result: I got this bro! Hello! Fatal error: Call to protected method ChildClass::youNeedToProtectThisMan() */
{ $this->tidyconfig = array('indent' => false, 'clean' => true, 'merge-divs' => false, 'quote-marks' => true, 'drop-empty-paras' => false, 'markup' => false, 'output-xhtml' => true, 'wrap' => 0); } public abstract function run(); public function getURL($url) { $data = "awerawer"; // in my code, $data is downloaded from a site $tidy = new tidy(); $tidy->parseString($data, $this->tidyconfig, 'utf8'); $tidy->cleanRepair(); return $tidy; } } class ChildClass extends BaseClass { public function __construct() { parent::__construct(); } public function run() { $result = $this->getURL('awer'); if ($result === null) { echo "\tError:\n"; } var_dump((string) $result); } } $instance = new ChildClass(); $instance->run();
<?php class TestClass { public static function createInstance() { return new static(); } } class ChildClass extends TestClass { } $testClass = TestClass::createInstance(); $childClass = ChildClass::createInstance(); echo get_class($testClass) . "\n"; echo get_class($childClass) . "\n"; ?> ==DONE==
<?php include 'Class-parent.php'; include 'Class-child.php'; /* * Working with inheritence of Object oriented programming * To change this template file, choose Tools | Templates * Developed by Anam */ /** * Call the static method in * the parent class */ echo ParentClass::loveEachOther(); $ChildClass = new ChildClass(); $ChildClass->parentProductFunction(); echo $ChildClass->override();
<?php require_once 'aExample-abstract.php'; require_once 'ChildClass-class.php'; $ChildClass = new ChildClass(); $ChildClass->setTaxRate(6); $messages; $messages[] = 'De belastingsprocent bedraagt ' . $ChildClass->getTaxRate() . '%'; $messages[] = 'De belastingsprocent werd berekend voor het land ' . $ChildClass->getCountry() . '.'; require_once 'voorbeeld-classes-abstract-view.php';
<?php class ParentClass { public function myFunctionA() { echo "parent function A call!<br>"; } } class ChildClass extends ParentClass { public function myFunctionB() { echo "<b>child function B calls parent function:</b> "; parent::myFunctionA(); } public function myFunctionA() { echo "child function A call!<br>"; } } $child = new ChildClass(); $child->myFunctionB(); echo "<hr>"; $child->myFunctionA();
public function getObjectVars() { return array_merge(parent::getObjectVars(), get_object_vars($this)); }
<?php class BaseClass { public function test() { echo "BaseClass::test() called<br>"; } public final function moreTesting() { echo "BaseClass::moreTesting() called<br>"; } } class ChildClass extends BaseClass { public function test() { echo "ChildClass::test() called<br>"; } } $ins = new ChildClass(); echo $ins->test() . '<br >'; echo $ins->moreTesting();