Beispiel #1
0
    public static function getCar($make, $model)
    {
        // Remember how to make the first character uppercase
        $className = ucfirst(strtolower($model));
        //echo '$className=' . $className;
        // How do you make sure a class exists?
        if (!class_exists($className)) {
            // How do you throw exceptions?
            throw new Exception($className . ' does not exist!');
        }
        // Remember how to dynamically instantiate objects
        $obj = new $className();
        $make = ucfirst($make);
        // How do you make sure the parent class is valid?
        if (!is_subclass_of($obj, $make)) {
            throw new Exception($className . ' is not a subclass of ' . $make);
        }
        return $obj;
    }
}
class Toyota
{
}
class Corolla extends Toyota
{
}
class Prius extends Toyota
{
}
$car = CarFactory::getCar('toyota', 'corolla');
print_r($car);
Beispiel #2
0
    {
        $model = ucfirst(strtolower($model));
        if (!class_exists($model)) {
            throw new Exception($model . ' is not a valid model');
        }
        $newCar = new $model();
        $parent = get_parent_class($newCar);
        $make = ucfirst(strtolower($make));
        //can also use is_subclass_of($child, $parent)
        if ($parent != $make) {
            throw new Exception($make . ' is not a valid make for this model');
        }
        return $newCar;
    }
}
class Toyota
{
}
class Corolla extends Toyota
{
}
class Prius extends Toyota
{
}
try {
    $prius = CarFactory::getCar('Honda', 'Prius');
    echo '<pre>';
    print_r($prius);
} catch (Exception $e) {
    echo $e->getMessage();
}
Beispiel #3
0
}
class ToyotaCorolla extends Car
{
}
class ToyotaCamry extends Car
{
}
class ToyotaCrx extends Car
{
}
class HondaCivic extends Car
{
}
class CarFactory
{
    public static function getCar($make, $model, $color)
    {
        $make = ucfirst($make);
        $model = ucfirst($model);
        $className = $make . $model;
        if (!class_exists($className)) {
            throw new Exception($className . ' does not exist!');
        }
        $classObj = new $className($make, $model, $color);
        return $classObj;
    }
}
// -----------------------------------------------------------
$carObject = CarFactory::getCar('toyota', 'crx', 'black');
echo '<pre>';
print_r($carObject);
Beispiel #4
0
        echo '<pre>';
        print_r($carObject);
        $parentClass = get_parent_class($carObject);
        // you will need to instantiate this prior
        if (ucfirst($make) != $parentClass) {
            throw new Exception($make . ' is not valid for ' . $model);
        }
        return $carObject;
        // print_r('$parentClass= ' . $parentClass);
        // echo '$className= ' . $className . '</br>';
    }
}
abstract class Toyota
{
}
class Corolla extends Toyota
{
}
class Camry extends Toyota
{
}
class Prius extends Toyota
{
}
try {
    $car = CarFactory::getCar('honda', 'corolla');
    echo '<pre>';
    print_r($car);
} catch (Exception $e) {
    echo '<font color="red">' . $e->getMessage() . '</font>';
}
Beispiel #5
0
class CarFactory
{
    public static function getCar($make, $model)
    {
        if ($make == 'corolla') {
            return new Corolla($model);
        } elseif ($make == 'camry') {
            return new Camry($model);
        } else {
            throw new NotImplementedException($make . ' is not defined!');
        }
    }
}
try {
    $corolla = CarFactory::getCar('corolla', 'DX');
    $camry = CarFactory::getCar('camry', 'LX');
    echo 'Corollas Torque: ' . $corolla->getTorque() . '<br/>';
    echo 'Camrys Torque: ' . $camry->getTorque() . '<br/>';
} catch (NotImplementedException $exceptionObject) {
    echo '<p style="color:red;">' . $exceptionObject->getMessage() . '</p>';
} catch (DeveloperException $devException) {
    // Something caught on fire
} catch (GooglePaymentFailedException $googleException) {
    // Send out SMS alerts, ring the phones, dim the lights
}
class UserException extends Exception
{
}
class DeveloperException extends Exception
{
}
Beispiel #6
0
        if (!class_exists($className)) {
            throw new Exception($className . ' has not been created!');
        }
        // Instantiate the class dynamically
        $carObject = new $className();
        $parentClass = get_parent_class($carObject);
        if (ucfirst($make) != $parentClass) {
            throw new Exception($make . ' is not valid for ' . $model);
        }
        return $carObject;
    }
}
abstract class Toyota
{
}
class Corolla extends Toyota
{
}
class Camry extends Toyota
{
}
class Prius extends Toyota
{
}
try {
    $car = CarFactory::getCar('toyota', 'civic');
    echo '<pre>';
    print_r($car);
} catch (Exception $whatevs) {
    echo '<font color="red">' . $whatevs->getMessage() . '</font>';
}