cvs: ZendEngine2 / ZEND_CHANGES zend_object_handlers.c
| From: | Stanislav Malyshev | Date: | Wed, 04 Sep 2002 15:03:41 +0000 |
| Subject: | cvs: ZendEngine2 / ZEND_CHANGES zend_object_handlers.c | ||
| Groups: | php.zend-engine.cvs | ||
| Request: | Send a blank email to php-zend-engine-cvs+get-402@lists.php.net to get a copy of this message | ||
stas Wed Sep 4 11:03:41 2002 EDT
Modified files:
/ZendEngine2 ZEND_CHANGES zend_object_handlers.c
Log:
Fix __call and add some docs
Index: ZendEngine2/ZEND_CHANGES
diff -u ZendEngine2/ZEND_CHANGES:1.36 ZendEngine2/ZEND_CHANGES:1.37
--- ZendEngine2/ZEND_CHANGES:1.36 Wed Sep 4 08:49:15 2002
+++ ZendEngine2/ZEND_CHANGES Wed Sep 4 11:03:41 2002
@@ -534,7 +534,57 @@
* __autoload(). TBD.
- * __call(), __get(), __set(). TBD.
+ * Method calls and property accesses can be overloaded by class
+ methods __call(), __get() and __set().
+ Examples:
+
+ <?php
+ class Setter {
+ var $n;
+ var $x = array("a"=>1,"b"=>2,"c"=>3);
+ function __get($nm) {
+ print "Getting [$nm]\n";
+ if(isset($this->x[$nm])) {
+ $r = $this->x[$nm];
+ print "Returning: $r\n";
+ return $r;
+ } else {
+ print "Nothing!\n";
+ }
+ }
+ function __set($nm, $val) {
+ print "Setting [$nm] to $val\n";
+ if(isset($this->x[$nm])) {
+ $this->x[$nm] = $val;
+ print "OK!\n";
+ } else {
+ print "Not OK!\n";
+ }
+ }
+ }
+
+ $foo = new Setter();
+ $foo->n = 1;
+ $foo->a = 100;
+ $foo->a++;
+ $foo->z++;
+ var_dump($foo);
+ ?>
+
+ <?php
+ class Caller {
+ var $x = array(1,2,3);
+ function __call($m, $a) {
+ print "Method $m called:\n";
+ var_dump($a);
+ return $this->x;
+ }
+ }
+
+ $foo = new Caller();
+ $a = $foo->test(1, "2", 3.4, true);
+ var_dump($a);
+ ?>
Changes in the Zend Engine 1.0
Index: ZendEngine2/zend_object_handlers.c
diff -u ZendEngine2/zend_object_handlers.c:1.10 ZendEngine2/zend_object_handlers.c:1.11
--- ZendEngine2/zend_object_handlers.c:1.10 Wed Sep 4 08:49:30 2002
+++ ZendEngine2/zend_object_handlers.c Wed Sep 4 11:03:41 2002
@@ -300,10 +300,10 @@
{
zval ***args;
zend_internal_function *func = (zend_internal_function *)EG(function_state_ptr)->function;
- zval method_name, method_args, method_result, __call_name;
- zval *method_name_ptr, *method_args_ptr, *method_result_ptr;
- zval **call_args[3];
- zval *retval = NULL;
+ zval method_name, method_args, __call_name;
+ zval *method_name_ptr, *method_args_ptr;
+ zval **call_args[2];
+ zval *method_result_ptr = NULL;
int i, call_result;
args = (zval ***)emalloc(ZEND_NUM_ARGS() * sizeof(zval **));
@@ -322,6 +322,11 @@
INIT_PZVAL(method_args_ptr);
array_init(method_args_ptr);
+// method_result_ptr = &method_result;
+// method_result.is_ref = 0;
+// method_result.refcount = 1;
+// ZVAL_NULL(method_result_ptr);
+
for(i=0; i<ZEND_NUM_ARGS(); i++) {
zval_add_ref(args[i]);
add_next_index_zval(method_args_ptr, *args[i]);
@@ -329,52 +334,38 @@
efree(args);
- method_result_ptr = &method_result;
- method_result.is_ref = 1;
- method_result.refcount = 1;
- ZVAL_NULL(method_result_ptr);
-
INIT_PZVAL(&__call_name);
ZVAL_STRINGL(&__call_name, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME)-1, 0);
- /* __call handler is called with three arguments:
+ /* __call handler is called with two arguments:
method name
array of method parameters
- return value for the method (by reference)
- it should return whether the call was successfull or not
*/
call_args[0] = &method_name_ptr;
call_args[1] = &method_args_ptr;
- call_args[2] = &method_result_ptr;
/* go call the __call handler */
call_result = call_user_function_ex(NULL,
&this_ptr,
&__call_name,
- &retval,
- 3, call_args,
+ &method_result_ptr,
+ 2, call_args,
0, NULL TSRMLS_CC);
- /* A lot of return values here. Let's sort them out:
- call_result is if call_user_function gone OK.
- retval shows if __call method went OK.
- method_result is the true result of the called method
+ /* call_result is if call_user_function gone OK.
+ method_result_ptr is the true result of the called method
*/
-
+ if(method_result_ptr) {
+ *return_value = *method_result_ptr;
+ zval_copy_ctor(return_value);
+ zval_ptr_dtor(&method_result_ptr);
+ }
+
if(call_result == FAILURE) {
zend_error(E_ERROR, "Could not call __call handler for class %s",
Z_OBJCE_P(this_ptr)->name);
}
-
- if (retval && zval_is_true(retval)) {
- *return_value = method_result;
- INIT_PZVAL(return_value);
- } else {
- zval_dtor(method_result_ptr);
- zend_error(E_ERROR, "Call to undefined method %s::%s()", Z_OBJCE_P(this_ptr)->name,
Z_STRVAL(method_name));
- }
- zval_ptr_dtor(&retval);
/* now destruct all auxiliaries */
zval_dtor(method_args_ptr);