public function baz(Test $other) { echo $other->foo . "\n"; // We can change the private property: $other->foo = 'hello'; var_dump($other->foo); // We can also call the private method: $other->bar(); }
public function baz(Test $other) { // Мы можем изменить закрытое свойство: $other->foo = 'hello'; var_dump($other->foo); // Мы также можем вызвать закрытый метод: $other->bar(); }
public function test() { $foo = new Test(); $this->assertTrue($foo->bar()); $this->assertFalse($foo->baz()); }
public function __call($method, $args) { } } function do_throw() { throw new Exception(); } try { Test::foo(do_throw()); } catch (Exception $e) { echo "Caught!\n"; } try { (new Test())->bar(do_throw()); } catch (Exception $e) { echo "Caught!\n"; } try { $f = function () { }; $f->__invoke(do_throw()); } catch (Exception $e) { echo "Caught!\n"; } try { $t = new Test(); $f->__invoke($t->bar(Test::foo(do_throw()))); } catch (Exception $e) { echo "Caught!\n"; }
<?php interface TestInterface { public function foo(); public function bar(array $bar); } class Test implements TestInterface { public function foo(...$args) { echo __METHOD__, "\n"; } public function bar(array $bar, ...$args) { echo __METHOD__, "\n"; } } $obj = new Test(); $obj->foo(); $obj->bar([]);