PHP属性__set()
来自《PHP与MySQL5程序设计》第2版 P103
<?php
/*
class Staff
{
var $name;
function __set($propName,$propValue)
{
echo "Nonexistent variable :\$$propName!";
}
}
$employee = new Staff();
$employee->name = "Mario";
$employee->title = "Executive Chef";
//Nonexistent variable :$title!
*/
//--------------------------------------------------
class Staff
{
var $name;
function __set($propName,$propValue)
{
$this->$propName = $propValue;
}
}
$employee = new Staff();
$employee->name = "Mario";
$employee->title = "Executive Chef";
echo "Name:"."$employee->name"."\n";
echo "Title:"."$employee->title";
?>
[ 本帖最后由 php 于 2007-11-19 17:51 编辑 ]