<?php
//Simple object to keep track of an Article
class ArchiveArticle
{
public $timestamp;
public $filename;
public $slug;
public $category;
public $status;
public $pubdate;
public $ranking;
public $newsgate_state;
public $newsgate_story_id;
public $newsgate_budget_id;
public $newsgate_text_id;
public $newsgate_profile;
public $taxonomy;
public $overline;
public $net_title;
public $headline;
public $teasertitle;
public $teasersummary;
public $subhead;
public $summary;
public $byline_name;
public $dateline;
public $paragraphs = array();
public $more_infos = array();
public $shirttail;
public $images = array();
}
$obj = new ArchiveArticle();
$obj->foo = 'bar'; //How can I stop this?
?>
在您的班级中添加:
public function __get($name) { return NULL; }
public function __set($name, $value) { }
说明:这两种方法将拦截对不存在和/或受保护/私有属性的任何访问,如果不存在,也会阻止其创建。
也可以看看http://php.net/manual/en/language.oop5.magic.php http://php.net/manual/en/language.oop5.magic.php
根据@DCoder的建议,你也可以这样做:
public function __get($name) {
throw new Exception("Property $name cannot be read");
}
public function __set($name, $value) {
throw new Exception("Property $name cannot be set");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)