- Allow variables to have both 'static' modifier and an access level.

NOTE:  This only works at the syntax level right now (parser).  It
         doesn't actually work as of yet - all statics are considered
         public for now
- Prevent users from putting more restrictions on methods in derived classes
  (i.e., you cannot make a public method private in a derived class, etc.)
This commit is contained in:
Zeev Suraski 2002-12-09 12:10:17 +00:00
parent 085f362ae3
commit 32b100e6cb
3 changed files with 22 additions and 17 deletions

View File

@ -1590,6 +1590,12 @@ static zend_bool do_inherit_method_check(zend_function *child, zend_function *pa
if ((child_flags & ZEND_ACC_ABSTRACT) && !(parent_flags & ZEND_ACC_ABSTRACT)) {
zend_error(E_COMPILE_ERROR, "Cannot make non abstract method %s::%s() abstract in class %s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, ZEND_FN_SCOPE_NAME(child));
}
/* Prevent derived classes from restricting access that was available in parent classes
*/
if ((child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
zend_error(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(child), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
}
return SUCCESS;
}
@ -2201,6 +2207,12 @@ void zend_do_declare_property(znode *var_name, znode *value TSRMLS_DC)
property->type = IS_NULL;
}
if (CG(access_type) & ZEND_ACC_STATIC) {
/* FIXME: Currently, ignores access type for static variables */
zend_hash_update(CG(active_class_entry)->static_members, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
return;
}
switch (CG(access_type)) {
case ZEND_ACC_PRIVATE:
{
@ -2245,9 +2257,6 @@ void zend_do_declare_property(znode *var_name, znode *value TSRMLS_DC)
case ZEND_ACC_PUBLIC:
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;
case ZEND_ACC_STATIC:
zend_hash_update(CG(active_class_entry)->static_members, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
break;
}
FREE_PNODE(var_name);
}

View File

@ -91,9 +91,7 @@ typedef struct _zend_brk_cont_element {
#define ZEND_ACC_STATIC 0x01
#define ZEND_ACC_ABSTRACT 0x02
/* No visibility is the same as public visibility.
* For inheritance no visibility means inheriting the visibility.
*/
/* The order of those must be kept - public < protected < private */
#define ZEND_ACC_PUBLIC 0x10
#define ZEND_ACC_PROTECTED 0x20
#define ZEND_ACC_PRIVATE 0x40

View File

@ -440,7 +440,7 @@ class_statement_list:
class_statement:
variable_modifier { CG(access_type) = $1.u.constant.value.lval; } class_variable_declaration ';'
variable_modifiers { CG(access_type) = $1.u.constant.value.lval; } class_variable_declaration ';'
| class_constant_declaration ';'
| method_modifiers T_FUNCTION { $2.u.opline_num = CG(zend_lineno); } is_reference T_STRING { zend_do_begin_function_declaration(&$2, &$5, 1, $4.op_type, $1.u.constant.value.lval TSRMLS_CC); } '('
parameter_list ')' '{' inner_statement_list '}' { zend_do_end_function_declaration(&$2 TSRMLS_CC); }
@ -449,28 +449,26 @@ class_statement:
| T_CLASS T_STRING extends_from '{' { zend_do_begin_class_declaration(&$1, &$2, &$3 TSRMLS_CC); } class_statement_list '}' { zend_do_end_class_declaration(&$1 TSRMLS_CC); }
;
variable_modifier:
access_modifier { $$ = $1; }
| T_VAR { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
| T_STATIC { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
variable_modifiers:
non_empty_access_modifiers { $$ = $1; }
| T_VAR { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
;
method_modifiers:
/* empty */ { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
| non_empty_method_modifiers { $$ = $1; if (!($$.u.constant.value.lval & ZEND_ACC_PPP_MASK)) { $$.u.constant.value.lval |= ZEND_ACC_PUBLIC; } }
| non_empty_access_modifiers { $$ = $1; if (!($$.u.constant.value.lval & ZEND_ACC_PPP_MASK)) { $$.u.constant.value.lval |= ZEND_ACC_PUBLIC; } }
;
non_empty_method_modifiers:
T_STATIC { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
| access_modifier { $$ = $1; }
| non_empty_method_modifiers T_STATIC { $$.u.constant.value.lval = $1.u.constant.value.lval | ZEND_ACC_STATIC; }
| non_empty_method_modifiers access_modifier { $$.u.constant.value.lval = zend_do_verify_access_types(&$1, &$2); }
non_empty_access_modifiers:
access_modifier { $$ = $1; }
| non_empty_access_modifiers access_modifier { $$.u.constant.value.lval = zend_do_verify_access_types(&$1, &$2); }
;
access_modifier:
T_PUBLIC { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
| T_PROTECTED { $$.u.constant.value.lval = ZEND_ACC_PROTECTED; }
| T_PRIVATE { $$.u.constant.value.lval = ZEND_ACC_PRIVATE; }
| T_STATIC { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
;
class_variable_declaration: