Board logo

标题: PHP属性__set() [打印本页]

作者: php    时间: 2007-11-19 17:49     标题: 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 编辑 ]




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0