title = "Executive Chef";//Nonexistent variable :$t ... 编程开发论坛 立华软件园,编程开发论坛 - Discuz! Archiver" />

查看完整版本: PHP属性__set()

php 2007-11-19 17:49

PHP属性__set()

来自《PHP与MySQL5程序设计》第2版 P103

[color=#000000][color=#00cc00][/color][color=#00cc00][color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color][color=#0000ff]php[/color]
[color=#ff9900]/*
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!
*/[/color]
[color=#ff9900]//--------------------------------------------------
[/color]

[color=#0000ff]class[/color] Staff
[color=#0000cc]{[/color]
    [color=#0000ff]var[/color] [color=#0000ff]$[/color][color=#008080]name[/color][color=#0000cc];[/color]
    [color=#0000ff]function[/color] __set[color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]propName[/color][color=#0000cc],[/color][color=#0000ff]$[/color][color=#008080]propValue[/color][color=#0000cc])[/color]
    [color=#0000cc]{[/color]
        [color=#0000ff]$[/color][color=#008080]this[/color][color=#0000cc]-[/color][color=#0000cc]>[/color][color=#0000ff]$[/color][color=#008080]propName[/color] [color=#0000cc]=[/color] [color=#0000ff]$[/color][color=#008080]propValue[/color][color=#0000cc];[/color]
    [color=#0000cc]}[/color]
[color=#0000cc]}[/color]
[color=#0000ff]$[/color][color=#008080]employee[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] Staff[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]employee[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]name [color=#0000cc]=[/color] [color=#ff00ff]"Mario"[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]employee[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]title [color=#0000cc]=[/color] [color=#ff00ff]"Executive Chef"[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Name:"[/color][color=#0000cc].[/color][color=#ff00ff]"$employee->name"[/color][color=#0000cc].[/color][color=#ff00ff]"\n"[/color][color=#0000cc];[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"Title:"[/color][color=#0000cc].[/color][color=#ff00ff]"$employee->title"[/color][color=#0000cc];[/color]

[color=#0000cc]?[/color][color=#0000cc]>[/color][/color]
[/color][/color]

[[i] 本帖最后由 php 于 2007-11-19 17:51 编辑 [/i]]
页: [1]
查看完整版本: PHP属性__set()