2014. szeptember 15., hétfő

Konstruktorok használata

<?php
class Item {
    private $name;

    function __construct( $name="item", $code=0 ) {
        $this->name = $name;
        $this->code = $code;
    }
   
    function getName() {
        return $this->name;
    }
}

class PriceItem extends Item {
    private $price;

    function __construct( $name, $code, $price ) {
        parent::__construct( $name, $code );
        $this->price = $price;
    }

    function getName() {
        return "(price) ".parent::getName();
    }
}

$item = new PriceItem( "widget", 5442, 5.20 );
print $item->getName();
// outputs "(price) widget"

?>

Eredmény:
(price) widget

Nincsenek megjegyzések:

Megjegyzés küldése