Re: private, protected, readonly, public

From: Date: Sun, 14 May 2006 09:05:29 +0000
Subject: Re: private, protected, readonly, public
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hello Jason,

Sunday, May 14, 2006, 4:34:03 AM, you wrote:

> Hello Marcus,

>   class x
>   {
>      public readonly $xyz;
>      protected readonly $abc;
>   }

>   Definitions:
>   - public readonly    - outside class can only read, not write.
>   - protected readonly - subclass can only read, not write.
>   - private readonly   - does not make sense - do not support.

>   How difficult would it be to build this into the PHP engine?

> -- 
> Best regards,
>  Jason                            mailto:[email protected]

> Saturday, May 13, 2006, 5:27:34 AM, you wrote:

MB>> Hello Etienne,

MB>> Friday, May 12, 2006, 2:11:38 PM, you wrote:

>>> Hi,

>>> my 2c:

>>> 1) it shouldn't replace the visibility definition: we could also have
>>> protected readonly properties.

MB>> same here visibility and read/write control are two seperate things.

>>> 3) how would you check if the property if readonly ? Trying it could
>>> result to a Fatal error as you wanted. You would then need a
>>> isReadonly() method/function: the function call wouldn't be spared.

MB>> We could add this to reflection api easily.

Here's your diff to play around :-)
The impact on runtime is a single additional integer check for protected
properties and  an additional check for private proeprties where property
access would normally fail. For this 5 minute patch i chose the key word
'readonly' as supposed. Actually writing the mail took much longer than
brewing the patch and yes i din't care for syntax right now.

php -r 'class T{private readonly $x = 42;} $obj = new T; var_dump($obj->x);'
int(42)

Or readable:

<?php
class Test {
  private readonly $x = 42;
}

$obj = new Test;
var_dump($obj->x);
?>

As we have the 'Property overloading RFC' on the 6.0 aganda
we should probably move part of the stuff to 5.2 already and
start with the public read access.

http://oss.backendmedia.com/PhP60
http://www.zend.com/zend/week/week248.php#Heading3


Best regards,
 Marcus


Index: Zend/zend_compile.h =================================================================== RCS file: /repository/ZendEngine2/zend_compile.h,v retrieving revision 1.316.2.8.2.2 diff -u -p -d -r1.316.2.8.2.2 zend_compile.h --- Zend/zend_compile.h 11 May 2006 21:07:39 -0000 1.316.2.8.2.2 +++ Zend/zend_compile.h 14 May 2006 08:58:46 -0000 @@ -139,6 +139,9 @@ typedef struct _zend_try_catch_element { /* deprecation flag */ #define ZEND_ACC_DEPRECATED 0x40000 +/* property handling control */ +#define ZEND_ACC_PUB_READ 0x80000 + char *zend_visibility_string(zend_uint fn_flags); Index: Zend/zend_language_parser.y =================================================================== RCS file: /repository/ZendEngine2/zend_language_parser.y,v retrieving revision 1.160.2.4.2.1 diff -u -p -d -r1.160.2.4.2.1 zend_language_parser.y --- Zend/zend_language_parser.y 11 May 2006 21:07:39 -0000 1.160.2.4.2.1 +++ Zend/zend_language_parser.y 14 May 2006 08:58:46 -0000 @@ -115,7 +115,7 @@ %token T_THROW %token T_USE %token T_GLOBAL -%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC +%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC T_READONLY %token T_VAR %token T_UNSET %token T_ISSET @@ -500,8 +500,9 @@ method_body: ; variable_modifiers: - non_empty_member_modifiers { $$ = $1; } - | T_VAR { Z_LVAL($$.u.constant) = ZEND_ACC_PUBLIC; } + non_empty_member_modifiers { $$ = $1; } + | non_empty_member_modifiers T_READONLY { $$ = $1; Z_LVAL($$.u.constant) |= ZEND_ACC_PUB_READ; } + | T_VAR { Z_LVAL($$.u.constant) = ZEND_ACC_PUBLIC; } ; method_modifiers: Index: Zend/zend_language_scanner.l =================================================================== RCS file: /repository/ZendEngine2/zend_language_scanner.l,v retrieving revision 1.131.2.11 diff -u -p -d -r1.131.2.11 zend_language_scanner.l --- Zend/zend_language_scanner.l 13 Apr 2006 13:48:28 -0000 1.131.2.11 +++ Zend/zend_language_scanner.l 14 May 2006 08:58:46 -0000 @@ -1063,6 +1063,10 @@ NEWLINE ("\r"|"\n"|"\r\n") return T_PUBLIC; } +<ST_IN_SCRIPTING>"readonly" { + return T_READONLY; +} + <ST_IN_SCRIPTING>"unset" { return T_UNSET; } Index: Zend/zend_object_handlers.c =================================================================== RCS file: /repository/ZendEngine2/zend_object_handlers.c,v retrieving revision 1.135.2.6.2.2 diff -u -p -d -r1.135.2.6.2.2 zend_object_handlers.c --- Zend/zend_object_handlers.c 10 May 2006 21:12:48 -0000 1.135.2.6.2.2 +++ Zend/zend_object_handlers.c 14 May 2006 08:58:47 -0000 @@ -150,9 +150,9 @@ static int zend_verify_property_access(z case ZEND_ACC_PUBLIC: return 1; case ZEND_ACC_PROTECTED: - return zend_check_protected(ce, EG(scope)); + return (property_info->flags & ZEND_ACC_PUB_READ) ? 1 : zend_check_protected(ce, EG(scope)); case ZEND_ACC_PRIVATE: - if (ce==EG(scope) && EG(scope)) { + if ((ce == EG(scope) && ce) || (property_info->flags & ZEND_ACC_PUB_READ)) { return 1; } else { return 0;

Thread (92 messages)

« previous php.internals (#23341) next »