public function testSomething()
 {
     $example = new Example();
     $this->assertEquals($example->foo(), 'bar');
 }
示例#2
0
class ExampleParent
{
    private $hello_world = "hello foo\n";
    public function foo()
    {
        echo $this->hello_world;
    }
}
class Example extends ExampleParent
{
    use ExampleTrait;
}
trait ExampleTrait
{
    /**
     *
     */
    private $hello_world = "hello bar\n";
    /**
     *
     */
    public $prop = "ops";
    public function bar()
    {
        echo $this->hello_world;
    }
}
$x = new Example();
$x->foo();
$x->bar();
<?php

class Example
{
    function foo()
    {
        echo "this is foo\n";
    }
    function bar()
    {
        echo "this is bar\n";
    }
    function __call($name, $args)
    {
        echo "tried to handle unknown method {$name}\n";
        if ($args) {
            echo "it had arguments: ", implode(', ', $args), "\n";
        }
    }
}
$example = new Example();
$example->foo();
// prints "this is foo"
$example->bar();
// prints "this is bar"
$example->grill();
// prints "tried to handle unknown method grill"
$example->ding("dong");
// prints "tried to handle unknown method ding"
// prints "it had arguments: dong
<?php

interface ExampleInterface
{
    public function foo() : string;
}
class Base
{
    protected $bar = 'bar';
}
class Example extends Base implements ExampleInterface
{
    public function __construct()
    {
        echo 'initialize';
    }
    public function foo() : string
    {
        return $this->bar;
    }
}
$example = new Example();
echo $example->foo();