2014. szeptember 15., hétfő

Osztály nyilvános tulajdonságokkal

<?php
class Item {
    var $name;
    var $code;
    var $productString;

    function Item( $name="item", $code=0 ) {
        $this->code = $code;
        $this->setName( $name );
    }

    function getProductString() {
        return $this->productString;
    }
   
    function setName( $n ) {
        $this->name = $n;
        $this->productString = $this->name." ".$this->code;
    }

    function getName() {
        return $this->name;
    }
}

$item = new Item("widget", 5442);
print $item->getProductString();
// outputs "widget 5442"

print "<br />";

$item->name = "widget-upgrade";
print $item->getProductString();
// outputs "widget 5442", not "widget-upgrade 5442"
// Mivel nem a setName függvénnyel állítottuk be a nevét, nem annak megfelelő lesz.
?>
Eredmény:
widget 5442
widget 5442

Nincsenek megjegyzések:

Megjegyzés küldése