]> granicus.if.org Git - php/commitdiff
- Experimental support for private members.
authorAndi Gutmans <andi@php.net>
Thu, 21 Feb 2002 11:50:44 +0000 (11:50 +0000)
committerAndi Gutmans <andi@php.net>
Thu, 21 Feb 2002 11:50:44 +0000 (11:50 +0000)
<?
class MyClass {
private $Hello = "Hello, World!\n";

function printHello()
{
print $this->Hello;
}
}

class MyClass2 extends MyClass {
function printHello()
{
MyClass::printHello(); /* Should print */
print $this->Hello; /* Shouldn't print out anything */
}
}

$obj = new MyClass();
print $obj->Hello; /* Shouldn't print out anything */
$obj->printHello(); /* Should print */

$obj = new MyClass2();
print $obj->Hello; /* Shouldn't print out anything */
$obj->printHello();
?>

Zend/zend.c
Zend/zend.h
Zend/zend_API.c
Zend/zend_compile.c
Zend/zend_language_parser.y
Zend/zend_language_scanner.l
Zend/zend_object_handlers.c
Zend/zend_opcode.c

index ecbb4d828b8decd8f4b08e3f6471eede0afa3445..67f498bd79fff193d05a38a76a7ec580ba1f5b85 100644 (file)
@@ -251,6 +251,7 @@ static void register_standard_class(void)
        zend_standard_class_def.name = zend_strndup("stdClass", zend_standard_class_def.name_length);
        zend_standard_class_def.parent = NULL;
        zend_hash_init_ex(&zend_standard_class_def.default_properties, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
+       zend_hash_init_ex(&zend_standard_class_def.private_properties, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
        zend_standard_class_def.static_members = (HashTable *) malloc(sizeof(HashTable));
        zend_hash_init_ex(zend_standard_class_def.static_members, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
        zend_hash_init_ex(&zend_standard_class_def.constants_table, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
index 9791075d6863aa6ee5f77bc29bd943fd3aeb7048..98501df6cac6c6036d07ee1da861d34cd686ad4a 100644 (file)
@@ -270,6 +270,7 @@ struct _zend_class_entry {
 
        HashTable function_table;
        HashTable default_properties;
+       HashTable private_properties; /* This is only needed at compile-time */
        HashTable class_table;
        HashTable *static_members;
        HashTable constants_table;
index f6121a8cd3ea404d38a9749905b10b59d48ebd17..7e03e22d4fa4a070a6a151246d86c2e421c7a2b8 100644 (file)
@@ -1224,6 +1224,7 @@ ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *class_
        *class_entry->refcount = 1;
        class_entry->constants_updated = 0;
        zend_hash_init(&class_entry->default_properties, 0, NULL, ZVAL_PTR_DTOR, 1);
+       zend_hash_init(&class_entry->private_properties, 0, NULL, ZVAL_PTR_DTOR, 1);
        class_entry->static_members = (HashTable *) malloc(sizeof(HashTable));
        zend_hash_init(class_entry->static_members, 0, NULL, ZVAL_PTR_DTOR, 1);
        zend_hash_init(&class_entry->constants_table, 0, NULL, ZVAL_PTR_DTOR, 1);
index 143ca3589e19535619b46619b4430c73549c6303..96a92b73567a6a1abfe4e5811511279a2d219db9 100644 (file)
@@ -1366,6 +1366,7 @@ static void create_class(HashTable *class_table, char *name, int name_length, ze
        zend_hash_init(&new_class_entry.function_table, 10, NULL, ZEND_FUNCTION_DTOR, 0);
        zend_hash_init(&new_class_entry.class_table, 10, NULL, ZEND_CLASS_DTOR, 0);
        zend_hash_init(&new_class_entry.default_properties, 10, NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_init(&new_class_entry.private_properties, 10, NULL, ZVAL_PTR_DTOR, 0);
        ALLOC_HASHTABLE(new_class_entry.static_members);
        zend_hash_init(new_class_entry.static_members, 10, NULL, ZVAL_PTR_DTOR, 0);
        zend_hash_init(&new_class_entry.constants_table, 10, NULL, ZVAL_PTR_DTOR, 0);
@@ -1520,6 +1521,7 @@ ZEND_API int do_bind_function_or_class(zend_op *opline, HashTable *function_tabl
                                        (*ce->refcount)--;
                                        zend_hash_destroy(&ce->function_table);
                                        zend_hash_destroy(&ce->default_properties);
+                                       zend_hash_destroy(&ce->private_properties);
                                        zend_hash_destroy(ce->static_members);
                                        zend_hash_destroy(&ce->constants_table);
                                        return FAILURE;
@@ -1856,6 +1858,7 @@ void zend_do_begin_class_declaration(znode *class_token, znode *class_name, znod
        zend_hash_init(&new_class_entry.function_table, 10, NULL, ZEND_FUNCTION_DTOR, 0);
        zend_hash_init(&new_class_entry.class_table, 10, NULL, ZEND_CLASS_DTOR, 0);
        zend_hash_init(&new_class_entry.default_properties, 10, NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_init(&new_class_entry.private_properties, 10, NULL, ZVAL_PTR_DTOR, 0);
        ALLOC_HASHTABLE(new_class_entry.static_members);
        zend_hash_init(new_class_entry.static_members, 10, NULL, ZVAL_PTR_DTOR, 0);
        zend_hash_init(&new_class_entry.constants_table, 10, NULL, ZVAL_PTR_DTOR, 0);
@@ -1963,6 +1966,20 @@ void zend_do_end_class_declaration(znode *class_token TSRMLS_DC)
        }
 }
 
+void mangle_private_property_name(char **dest, int *dest_length, char *src1, int src1_length, char *src2, int src2_length)
+{
+       char *priv_name;
+       int priv_name_length;
+
+       priv_name_length = 1 + src1_length + 1 + src2_length;
+       priv_name = emalloc(priv_name_length+1);
+       priv_name[0] = '\0';
+       memcpy(priv_name + 1, src1, src1_length+1);
+       memcpy(priv_name + 1 + src1_length + 1, src2, src2_length+1);
+
+       *dest = priv_name;
+       *dest_length = priv_name_length;
+}
 
 void zend_do_declare_property(znode *var_name, znode *value, int declaration_type TSRMLS_DC)
 {
@@ -1978,6 +1995,19 @@ void zend_do_declare_property(znode *var_name, znode *value, int declaration_typ
        }
 
        switch (declaration_type) {
+               case T_PRIVATE:
+                       {
+                               char *priv_name;
+                               int priv_name_length;
+
+                               mangle_private_property_name(&priv_name, &priv_name_length, CG(active_class_entry)->name, CG(active_class_entry)->name_length, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len);
+                               zend_hash_update(&CG(active_class_entry)->default_properties, priv_name, priv_name_length+1, &property, sizeof(zval *), NULL);
+                               efree(priv_name);
+
+                               property->refcount++;
+                               zend_hash_update(&CG(active_class_entry)->private_properties, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
+                               break;
+                       }
                case T_VAR:
                        zend_hash_update(&CG(active_class_entry)->default_properties, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
                        break;
@@ -2010,6 +2040,17 @@ void zend_do_fetch_property(znode *result, znode *object, znode *property TSRMLS
                        SET_UNUSED(opline_ptr->op2);
                        opline_ptr->op2.u.EA.type = ZEND_FETCH_FROM_THIS;
 
+                       if ((opline_ptr->op1.op_type == IS_CONST) && zend_hash_exists(&CG(active_class_entry)->private_properties, opline_ptr->op1.u.constant.value.str.val, opline_ptr->op1.u.constant.value.str.len+1)) {
+                               char *priv_name;
+                               int priv_name_length;
+
+                               mangle_private_property_name(&priv_name, &priv_name_length, CG(active_class_entry)->name, CG(active_class_entry)->name_length, opline_ptr->op1.u.constant.value.str.val, opline_ptr->op1.u.constant.value.str.len);
+
+                               STR_FREE(opline_ptr->op1.u.constant.value.str.val);
+                               opline_ptr->op1.u.constant.value.str.val = priv_name;
+                               opline_ptr->op1.u.constant.value.str.len = priv_name_length;
+                       }
+
                        *result = opline_ptr->result;
                        return;
                }
index 30f04d874ae132d043bf0c5e5bed7591b3efcf23..8975871f37aa4ec8a2ac85e5a37997dba6ef5c3c 100644 (file)
 %token T_USE
 %token T_GLOBAL
 %token T_STATIC
+%token T_PRIVATE
 %token T_VAR
 %token T_UNSET
 %token T_ISSET
@@ -421,6 +422,7 @@ class_variable_decleration:
 class_decleration_type:
                T_VAR           { $$.op_type = T_VAR; }
        |       T_STATIC        { $$.op_type = T_STATIC; }
+       |       T_PRIVATE       { $$.op_type = T_PRIVATE; }
 ;
 
 class_constant_decleration:
index 528d8cfdb090cf4b4229fb1d69676f1296ac0599..2a615ff27b7658fd6427a3a47edbf4a76e94b034 100644 (file)
@@ -705,6 +705,10 @@ NEWLINE ("\r"|"\n"|"\r\n")
        return T_STATIC;
 }
 
+<ST_IN_SCRIPTING>"private" {
+       return T_PRIVATE;
+}
+
 <ST_IN_SCRIPTING>"unset" {
        return T_UNSET;
 }
index 44d2526326a483efb86ee043f4c987be33f0b272..9db2cbce5a99aa33ad7e312537d117bf69084066 100644 (file)
@@ -5,7 +5,7 @@
 #include "zend_objects.h"
 #include "zend_object_handlers.h"
 
-#define DEBUG_OBJECT_HANDLERS 1
+#define DEBUG_OBJECT_HANDLERS 0
 
 static HashTable *zend_std_get_properties(zval *object TSRMLS_DC)
 {
index 85e5904b11610ee7f1c2cdd8b5c068c5a551cf0e..d99506511984ad792ad2c95a81c7f7f6171e7998 100644 (file)
@@ -112,6 +112,7 @@ ZEND_API void destroy_zend_class(zend_class_entry *ce)
        switch (ce->type) {
                case ZEND_USER_CLASS:
                        zend_hash_destroy(&ce->default_properties);
+                       zend_hash_destroy(&ce->private_properties);
                        zend_hash_destroy(ce->static_members);
                        efree(ce->name);
                        efree(ce->refcount);
@@ -122,6 +123,7 @@ ZEND_API void destroy_zend_class(zend_class_entry *ce)
                        break;
                case ZEND_INTERNAL_CLASS:
                        zend_hash_destroy(&ce->default_properties);
+                       zend_hash_destroy(&ce->private_properties);
                        zend_hash_destroy(ce->static_members);
                        free(ce->name);
                        free(ce->refcount);