本文整理汇总了PHP中Car::getStatus方法的典型用法代码示例。如果您正苦于以下问题:PHP Car::getStatus方法的具体用法?PHP Car::getStatus怎么用?PHP Car::getStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Car
的用法示例。
在下文中一共展示了Car::getStatus方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getStatus
}
public function getStatus()
{
$this->doStuff();
// can call the protected function
return $this->status;
}
function __construct($c = 'blue')
{
$this->color = $c;
}
}
// create an instance of Car class
$myCar = new Car();
echo '<p>' . $myCar->color . '</p>';
echo '<p>' . $myCar->getStatus() . '</p>';
echo '<p>' . $myCar->doMoreStuff() . '</p>';
// works
echo '<p>' . $myCar->flag . '</p>';
// doesn't work
echo '<p>' . $myCar::$flag . '</p>';
// works
echo '<p>' . Car::$flag . '</p>';
// must reference the class::property
class SUV extends Car
{
public $has4WD = 'yes';
}
$newCar = new SUV('black');
echo '<p>' . $newCar->color . '</p>';
echo '<p>' . $newCar->getStatus() . '</p>';
示例2: getStatus
public function getStatus()
{
// child classes can still access parent methods using parent::
return parent::getStatus();
}
示例3: startEngine
var $model = '';
protected $status = 'off';
function startEngine()
{
$this->status = 'running';
// if engine were an object inside the Car object
//$this->engine->status = 'running';
}
function getStatus()
{
return $this->status;
}
}
$myCar = new Car();
$myCar->color = 'black';
$myCar->make = 'Mercedes';
$myCar->model = '725L';
echo "<p>My car will be a {$myCar->color} {$myCar->make} {$myCar->model}.</p>";
//$myCar->status = 'running'; // doesn't work because $status is protected
echo '<p>Current car status: ' . $myCar->getStatus() . '.</p>';
$myCar->startEngine();
// start 'er up
echo '<p>Current car status: ' . $myCar->getStatus() . '.</p>';
$yourCar = new Car();
echo '<p>Your car\'s status: ' . $yourCar->getStatus() . '.</p>';
// Accessing sub-objects might look something like this:
//$myCar->engine->start();
?>
</body>
</html>
示例4: getStatus2
static $doesHave4WD = true;
public function getStatus2()
{
return $this->status;
}
// static members are accessed directly from the class using ::
// they are not accessible from class instances
static function has4WD()
{
// can't use $this in a static method because it's not associated
// with an instance
//return $this->has4WD;
// we also can't access non-static members directly from the class
//return SUV::has4WD;
// note that the static variable has the $ in the name
return SUV::$doesHave4WD;
}
}
$yourCar = new Car(array('make' => 'Cadillac', 'model' => 'El Dorado', 'color' => 'pearl'));
$myCar = new SUV();
echo '<p>Your car color is ' . $yourCar->color . '</p>';
echo '<p>Your car ' . (isset($yourCar->has4WD) && $yourCar->has4WD ? 'has' : 'does not have') . ' four-wheel drive.</p>';
echo '<p>My car ' . (isset($myCar->has4WD) && $myCar->has4WD ? 'has' : 'does not have') . ' four-wheel drive.</p>';
echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>';
echo '<p>My car status is ' . $myCar->getStatus() . '</p>';
// generates a warning because $status is private
echo '<p>My car status is ' . $myCar->getStatus2() . '</p>';
$myCar->has4WD();
// can't access static properties
echo SUV::has4WD();
// access the static member directly from the class
示例5: setStatus
class Car
{
// variables inside a class are called properties or attributes
public $make = null;
public $model = null;
public $color = null;
protected $status = 'off';
// functions inside a class are called methods
public function setStatus($value)
{
// '$this' refers to the current instance
$this->status = $value;
}
public function getStatus()
{
return $this->status;
}
}
// we create instances (actual objects) of a class using 'new'
$yourCar = new Car();
$myCar = new Car();
// use the '->' operator to access an object's members
$yourCar->color = 'red';
$yourCar->color = 'blue';
echo '<p>Your car color is ' . $yourCar->color . '.</p>';
//echo $yourCar->status;
echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>';
$yourCar->setStatus('running');
echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>';
echo '<p>My car status is ' . $myCar->getStatus() . '</p>';