PHP只读属性类的定义
1 <?php
2 /**
3 * 只读属性类的定义
4 *
5 * Project: Tiwer Hypertext Preprocessor Development Frameworks
6 *
7 * Site: http://wgw8299.cnblogs.com/
8 *
9 * $Id: variables.php 241 2010-09-14 13:52:01 wgw8299 $
10 *
11 * Copyright (C) 2008-2009 Tiwer All Rights Reserved.
12 */
13 class ClassName {
14
15 /* private variables */
16 private $aValue;
17
18 /* public members __set */
19 public function __set($name, $value) {
20 if ( isset( $this->$name ) ) {
21 trigger_error( $name . 'is readonly !', E_USER_NOTICE );
22 } else {
23 $this->$name = $value;
24 }
25 }
26
27 /* public members __get */
28 public function __get($name) {
29 if( isset( $this->$name ) ) {
30 return $this->$name;
31 } else {
32 trigger_error( $name . ' variables undefined', E_USER_NOTICE );
33 }
34 }
35 }
36
37 /* new object */
38 $object = new ClassName();
39
40 /* Test Use Cases */
41 $object->aValue = 'wgw8299';
42 echo $object->aVaalue;
43 ?>
Tiwer
浙公网安备 33010602011771号