Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for asymmetric visibility for properties #43

Merged
merged 6 commits into from
Aug 26, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Aug 24, 2024

Example

Given the following:

use lang\Reflection;

class Person {
  public private(set) string $name= 'Test';
}
 
$property= Reflection::type(Person::class)->property('name');

The modifiers class includes private(set) and protected(set) support:

$property->modifiers(); // lang.reflect.Modifiers<public private(set)>

The isPublic(), isProtected() and isPrivate() methods optionally support "get" or "set":

$property->modifiers()->isPublic('get');  // true
$property->modifiers()->isPrivate('set'); // true

The modifiers() method can be used with "get" or "set" to produce views on the respective visibility:

$property->modifiers('get')->isPublic();  // true
$property->modifiers('set')->isPrivate(); // true

Tests

The following configurations have run all unit tests successfully:

See also

@thekid thekid added the enhancement New feature or request label Aug 24, 2024
thekid added a commit to xp-framework/compiler that referenced this pull request Aug 25, 2024
@thekid
Copy link
Member Author

thekid commented Aug 25, 2024

Here's the modifiers as returned by ReflectionProperty::getModifiers() in PHP 8.4 compiled from php/php-src#15063:

Declaration Modifiers Comment
class T { public int $prop; } 0x0001 1️⃣
class T { public public(set) int $prop; } 0x0001 2️⃣
class T { protected protected(set) int $prop; } 0x0002 2️⃣
class T { private private(set) int $prop; } 0x0004 2️⃣
class T { public protected(set) int $prop; } 0x0801
class T { public private(set) int $prop; } 0x1001
class T { proteced private(set) int $prop; } 0x1002

1️⃣ - For comparison with the next line
2️⃣ - Note that if get and set visibilities are the same, the set bit is emitted

private static $ASYMMETRIC;

static function __static() {
self::$ASYMMETRIC= method_exists(\ReflectionProperty::class, 'isPrivateSet');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be replaced by #[Runtime(version: '>=8.4')] once the PR is merged.

@thekid thekid merged commit fe96140 into main Aug 26, 2024
12 checks passed
@thekid thekid deleted the feature/asymmetric-visibility branch August 26, 2024 19:23
@thekid
Copy link
Member Author

thekid commented Aug 26, 2024

thekid added a commit that referenced this pull request Aug 27, 2024
@thekid
Copy link
Member Author

thekid commented Aug 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant