-
Notifications
You must be signed in to change notification settings - Fork 65
/
Base_Object.php
89 lines (74 loc) · 2.68 KB
/
Base_Object.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
**************************************************************************************************************************
** CORAL Common Module v. 1.0
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
**
** CORAL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
**
** CORAL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with CORAL. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************************************************************
*/
class Base_Object {
public function __construct(NamedArguments $arguments = NULL) {
if (method_exists($this, 'init')) {
if (!is_a($arguments, 'NamedArguments')) {
$arguments = new NamedArguments(array());
}
$this->init($arguments);
}
}
// An optional initializer to use instead of |__construct()|.
protected function init(NamedArguments $arguments) {
}
public function __destruct() {
if (method_exists($this, 'dealloc')) {
$this->dealloc();
}
}
// An optional method called on deconstruction instead of |__deconstruct()|.
protected function dealloc() {
}
// Setters are functions called |$instance->setPropertyName($value)|.
public function __set($name, $value) {
$methodName = 'set' . ucfirst($name);
$this->$methodName($value);
}
// Getters are functions called |$instance->propertyName()|.
public function __get($name) {
return $this->$name();
}
// Default setter uses declared properties.
protected function setValueForKey($key, $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
} else {
throw new Exception(sprintf( _("Cannot get value for undefined key '%s'."), $key ));
}
}
// Default getter uses declared properties.
protected function valueForKey($key) {
if (property_exists($this, $key)) {
return $this->$key;
} else {
throw new Exception(sprintf( _("Cannot get value for undefined key '%s'."), $key ));
}
}
// Call |valueForKey| and |setValueForKey| as default setter and getter.
public function __call($name, $arguments) {
if (preg_match('/^set[A-Z]/', $name)) {
$key = preg_replace('/^set/', '\1', $name);
$key = lcfirst($key);
$this->setValueForKey($key, $arguments[0]);
} else {
return $this->valueForKey($name);
}
}
}
?>