Exemplo n.º 1
0
<?php

//基本的工厂类
class Fruit
{
    function __autoload()
    {
        echo 'aaa';
    }
}
class FruitFactory
{
    public static function factory()
    {
        return new Fruit();
    }
}
$instance = FruitFactory::factory();
echo get_class($instance);
Exemplo n.º 2
0
            }
            if ($item['action'] == 'set') {
                $object->{$item['property']} = $item['value'];
            }
        }
        return $object;
    }
}
class Fruit
{
    private $name, $color;
    public $price;
    function __construct($name, $color)
    {
        $this->name = $name;
        $this->color = $color;
    }
    function setName($name)
    {
        $this->name = $name;
    }
    function say()
    {
        print "Hello, this is an {$this->color} {$this->name}, its price is {$this->price} RMB.";
    }
}
$fruit_factory = new FruitFactory('Fruit', 'Apple', 'red');
$fruit_factory->setName('Apple');
$fruit_factory->price = 5;
$apple = $fruit_factory->instance();
$apple->say();