Trait method overrides base class method and satisfies prototype --FILE-- <?php error_reporting(E_ALL); abstract class Base { public abstract function sayHello(array $a); } class SubClass extends Base { public function sayHello(array $a) { echo "World!\n"; } } $s = new SubClass(); $s->sayHello(array()); trait SayWorld { public function sayHello(Base $d) { echo 'World!'; } } class MyHelloWorld extends Base { use SayWorld; } $o = new MyHelloWorld(); $o->sayHello(array()); ?>
<?php abstract class BaseClass { public static function __callStatic($name, $arguments) { echo "Calling static method '{$name}' " . implode(', ', $arguments) . "\n"; } } class SubClass extends BaseClass { } SubClass::foo();
// 親クラスとなるSuperClassを定義する class SuperClass { public $a = '親クラスのプロパティ A'; public $b = '親クラスのプロパティ B'; public function show() { return '親クラスのメソッド: ' . $this - a . ' と ' . $this->b; } } // SuperClassを継承し、SubClassを定義する。これはSuperClassの子クラス class SubClass extends SuperClass { public $a = '子クラスのプロパティ A'; public function show() { return '子クラスのメソッド: ' . $this->a . ' と ' . $this - b; } } function h($string) { return htmlspecialChars($string, ENT_QUOTES, 'UTF-8'); } $obj = new SubClass(); echo h($obj->show()); ?> </div> </body> </html>
<?php error_reporting(-1); ini_set('display_errors', 'On'); require_once '../../lib/ClassLoader.php'; AClass::aFunction(); echo '<br />'; SubClass::SubFunction();
<?php /* * Signature mismatch during inheritance has been reclassified * as an E_WARNING error in PHP 7. A similar mismatch with an * interface or abstract method triggers a fatal error. */ class Base { public function greet() { echo 'Hello, world!'; } } class SubClass extends Base { public function greet($input) { echo "Hello, {$input}!"; } } $object = new SubClass(); $object->greet('David');
<?php class SubClass extends BaseClass { } abstract class BaseClass { public function __call($name, $arguments) { return $this->{$name}(); } private function foobar() { return 'okey'; } } $test = new SubClass(); echo $test->foobar();
/** * @requires PHP 7.0 * @expectedException \Error * @expectedExceptionMessageRegExp /Call to private method.+from context/ */ public function testCallParentPrivateMethodFromSubClass() { $this->expectOutputRegex('/Call to private method.+from context/'); $obj = new SubClass(); $obj->callParentPrivateMethod(); }