-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Class Constant Visibility Modifiers #1494
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
Conversation
@marcioAlmada thanks for linking that! When I hear back from @reeze and hear if he is ok with me adopting/working with him on the RFC I will add it to that page (or I will have to start my own) |
Hi @Sean-Der Thanks for your work. I didn't finish my patch but start to working on other things. You are welcome to working on the RFC. |
Thanks for the quick response @reeze are you interested in working with me on it? I totally understand if you are busy/not interested anymore, I just want to make sure I am not stomping on your work/ideas. thanks! |
Hi @Sean-Der Go ahead ;-) I will test your patch and try to contribute to your branch once I have time. |
One question for the RFC: why the syntax used in interface is different in class? could we make it consistent? I saw the constant defined in interface use interface ICache {
protected const PROTECTED = 0;
} but the constants defined in class are just: class Foo {
protected FOO = 0;
public BAR = 10;
} |
@c9s your 2nd example is not valid syntax, see https://3v4l.org/ST9Gp |
@staabm The code was copied from RFC and it's only in RFC. (this one https://wiki.php.net/rfc/class_const_visibility ) |
I think changing the code below (The RFC one): class Token {
// Constants default to public
const PUBLIC_CONST = 0;
// Constants then also can have a defined visibility
private PRIVATE_CONST = 0;
protected PROTECTED_CONST = 0;
public PUBLIC_CONST_TWO = 0;
} to this one is better: class Token {
// Constants default to public
const PUBLIC_CONST = 0;
// Constants then also can have a defined visibility
private const PRIVATE_CONST = 0;
protected const PROTECTED_CONST = 0;
public const PUBLIC_CONST_TWO = 0;
} |
I dont think its necessary. Constants are supposed to be public anyway, unless you mean read-only fields, which can be private or protected. |
So, is this going to be merged or not? One more question: If a constant is defined as protected constant in one interface, is it available to classes implemented with the interface? |
I believe the inconsistent examples in the RFC are just an oversight. It indeed should be: class { private const PRIVATE_CONST = 0; } // valid
class { private PRIVATE_CONST = 0; } // invalid!
For major language changes we have a voting phase. You can watch the votings through http://php-rfc-watch.beberlei.de |
Hey @marcioAlmada @c9s yep you caught a typo! Pretty embarrassing for such a light RFC, thank you 👍 @HallofFamer In PHP I don't think you can have read-only fields (properties?) now. There are a couple of use cases for a readonly member of a class constant, check out the conversation so far about this here |
Please consider adding |
@Furgas usually RFCs are hard work and authors tend to add reflection API after the discussion or voting phase, when they already know it's worth committing more time to it. |
Thanks for sticking up for me @marcioAlmada appreciate it, OpenSource is usually less than welcoming. Thanks @Furgas for being interested in this RFC! getDocComment is now implemented by 47bdc4a |
What will be your stance regarding ReflectionClass and it's public API? Currently:
You're changing this behavior, don't you? The "RFC impact" section must be explicit about it :) |
@marcioAlmada Yep those should be changed as well! I will make those changes and update the RFC, have plenty of failing tests also. Trying to hit the bare minimum to just get the RFC approved. Thanks for looking it all over! thanks! |
ce->constants_table = perealloc(ce->constants_table, sizeof(zval) * ce->constants_count, ce->type == ZEND_INTERNAL_CLASS); | ||
zend_hash_add_ptr(&ce->constants_info, name, const_info); | ||
} else { | ||
i_zval_ptr_dtor(&ce->constants_table[const_info->offset] ZEND_FILE_LINE_CC); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old behavior was to just update the value of the constant if it already existed, and some extensions in tree now depend on it (Date being one of them)
I thought better to keep the existing behavior, because maybe extensions outside php-src depend on this?
Hi, Also, there seems to be a conflict in trait resolution for constants and methods with same name (based on an example from http://news.php.net/php.internals/88906). What will this code do? trait TestTrait {
const foo = 'const';
function foo() { echo 'method'; }
}
class TestClass {
use TestTrait { foo as bar; }
} |
@Majkl578 that example was wrong (it was mine) sorry please ignore the trait stuff. I am sorry if the Reflection changes cause any issues for you! However, if the change was not made we would be missing out on important information (flags + docComments) It will be a while before this code ends up on production machines though (7.1 is a long time away) |
Hi!
First of all: currently, traits cannot have constants so the problem doesn't even exists https://3v4l.org/gCvYH. Even if we add constants to traits one day (and I hope this day never comes), we could have: trait TestTrait {
const foo = 'const';
function foo() { echo 'method'; }
}
class TestClass {
use TestTrait {
const foo as bar;
foo as bar;
}
} I don't see any issue here 😉 Regarding the Reflection API break, @nikic proposed an alternative. Instead of changing the return values of existing methods, we could just include Seems reasonable, even though I prefer the BC break. |
@marcioAlmada I prefer the BC break as well. My rationale is
But always happy to take to a vote :) |
I think it's better if you can squash all you changes into one commit... , there are lots of commits now |
@laruence done! Are you able to reproduce those test failures on a ZTS build? My CLI SAPI returns
But |
@Sean-Der The debug build on Travis also enabled opcache. That's likely where the issue is. |
Thanks for the clarification about traits, that's not an issue for now. I understand your reasons for breaking compatibility in relfection, but this is an excellent example of a change that becomes a nighmare for projects that need to keep compatibility with multiple versions of PHP. And since this RFC targets 7.1, it should also be noted that it violates PHP's release process, which prohibits userland API BC breaks in x.y+1.z versions. |
@Majkl578 That is a great point, what do you think of not breaking the old API at all and doing @marcioAlmada's suggestion? Adding two new subroutines |
And done! I implemented @marcioAlmada's suggestion, and no more BC in this commit anymore. AFAIK this is ready for review (and hopefully merge) if anyone has any suggestions/guidance on better tests or who to bother to get the merge process going would love to hear! |
@dstogov Hey! Would you mind reviewing the opcache changes, the entire test suite does pass though. thanks |
HashTable function_table; | ||
HashTable properties_info; | ||
HashTable constants_table; | ||
HashTable constants_info; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, it's going to be more efficient to introduce zend_class_constant structure and keep such structures in HashTable constants_table. We don't need separate "info" and "table" because, in opposite to properties, constants can't be changed in run-time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lucky to get a review out of you! Thanks for taking a careful look at this, and keeping php-src in top shape :)
If there is anything I can do code wise, just tell me. Would love to stay involved, but it is more efficient for you to work alone.
thanks
Please review another PR based on this one #1662 |
close this, focus on #1662 instead |
@reeze created an RFC here and I emailed internals here and didn't get any responses positive/negative.
This is a WIP and has test fails, and the API has not been finalized. Maybe it should be changed to mirror properties more, but this would cause more breakage.