<?php
class car {
private $_marque;
public function getMarka(){
$this->_marque;
}
public function setMarka($data){
return $this->_marque = $data;
}
}
$tachka = new car();
$tachka->setMarka('Мазда');
echo $tachka->getMarka();
<?php
class car {
private $_marque;
public function getMarka(){
$this->_marque;
}
public function setMarka($data){
return $this->_marque = $data;
}
}
$tachka = new car();
$tachka->setMarka('Мазда');
echo $tachka->getMarka();
Цитата |
class car { private $_marque; public function getMarka(){ return $this->_marque; } public function setMarka($data){ $this->_marque = $data; } } $tachka = new car(); $tachka->setMarka('Мазда'); echo $tachka->getMarka(); |
public function getMarka(){
$this->_marque;
}
class car {
private $_marque;
public function getMarka(){
return $this->_marque;
}
public function setMarka($data){
$this->_marque = $data;
}
}
class car {
private $_m = array();
public function __get($name) {
if(array_key_exists($name, $_m)
return $this->$_m[$name];
else
throw new Expception('no such element');
}
public function __set($name, $data) {
$this->_m[$name] = $data;
}
}
Цитата |
Что вы ввиду имели тут? Короче, вот так надо |
$car = new Car;
$car->brand = 'Mazda';
$car->engine_power = 110;
$car->color = 'Red';
echo $car->brand; // output Mazda
echo $car->engine_power; // 100
echo $car->color; // Red
Цитата (ABC @ 19.11.2013 - 05:30) | ||
А чем отличается от того, что я написал? |
Цитата |
Отвечайте соблюдая формат. |
Цитата |
А чем "родные" магические методы плохи __get и __set? |
Цитата |
if(array_key_exists($name, $_m) |
Цитата (dr.nomore @ 19.11.2013 - 01:27) |
if(array_key_exists($name, $_m) |
Цитата |
isset() не возвращает TRUE для ключей массива, указывающих на NULL, а array_key_exists() возвращает. |
Цитата |
Про try catch не понял. Объясните. |
/**
* Returns the item at the specified offset.
* This method is exactly the same as {@link offsetGet}.
* @param integer $index the index of the item
* @return mixed the item at the index
* @throws CException if the index is out of the range
*/
public function itemAt($index)
{
if(isset($this->_d[$index]))
return $this->_d[$index];
elseif($index>=0 && $index<$this->_c) // in case the value is null
return $this->_d[$index];
else
throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
array('{index}'=>$index)));
}