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

anon class objects #470

Closed
wants to merge 68 commits into from
Closed

anon class objects #470

wants to merge 68 commits into from

Conversation

krakjoe
Copy link
Member

@krakjoe krakjoe commented Sep 21, 2013

allow anonymous class objects

<?php
$var = new class {
    public $data;

    public function __construct($data) {
        $this->data = $data;
    }
} ("Hello World");

var_dump($var);

interface IObject {
    public function iMethod();
}

function my_function(IObject $object) {
    return $object->iMethod();
}

var_dump(my_function(new class implements IObject {
    public function iMethod() {
        return __METHOD__;
    }
}));
?>

Thoughts ??

@lstrojny
Copy link
Contributor

👍

@krakjoe
Copy link
Member Author

krakjoe commented Sep 22, 2013

Last commit makes the following possible:

class My {
    public function Test() {
        return new class{};
    }
}

@Majkl578
Copy link
Contributor

👍
I'd really like to see implementing interfaces / extending classes (especially abstract) anonymously in PHP, like (Java does)[http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html].

Some questions:

  • How would reflection handle these anonymous classes (when using ReflectionClass($obj))?
  • How would var_dump represent this object?
  • What would happen when using instanceof on this object?
  • ...

Also note that the new class syntax may interfere the PR #438 (or not?).

@krakjoe
Copy link
Member Author

krakjoe commented Sep 22, 2013

This supports the use of abstracts/interfaces/traits etc ...

new class implements Whatever {}
new class extends Something implements Whatever {}

is fine ...

Anonymous classes are normal classes without (programmer declared) names, so ReflectionClass/var_dump/instanceof will work as you expect

Don't see a reason for it to interfere with #438 ...

	make sure use of class_token is correct
	revert changes to znode typedef, reuse op.ptr
@lisachenko
Copy link
Contributor

Not sure that I like this proposal, because syntax is too bad, especially with defining new classes and interfaces inside functions. May be we choose a Java way of private|protected classes or interfaces inside the concrete class except anonymous classes? So classes and interfaces can be defined inside the body of class but will not be available for general use outside.

@kassner
Copy link

kassner commented Sep 23, 2013

Any clue about how object serialization will work with this?

@yagmikita
Copy link

Can you provide any examples where one can not handle the situation without anon class ? Don't you think this will break some of the SOLID paradigms ?

@feketegy
Copy link

How is this different from

$var = new stdClass;

?

@krakjoe
Copy link
Member Author

krakjoe commented Sep 23, 2013

Hi chaps, please see the RFC and @Internals for updates and answers ...

@kassner
Copy link

kassner commented Sep 23, 2013

@yagmikita Normally way I will expect serialize/unserialize working with any object that is not dependent of a resource. In this case, I will not able to unserialize an anon class, because there is not class name. Let me know if I could not explain well.

@HallofFamer
Copy link

I see, looks really nice, but whats the colon : used in front of the visibility modifier? Its just for testing purpose, right? o_o

@krakjoe
Copy link
Member Author

krakjoe commented Oct 1, 2013

Yeah, correct, it's gone now, we're over it ...

I'll be updating the rfc tomorrow with the new version information, couple of things left to do on it, though you should be able to build and pass tests ...

@HallofFamer
Copy link

Wow this is in voting phase now, excellent, cant wait to see how it ends up with.

@HallofFamer
Copy link

Why is the poll looking so horrible now? I dont remember there was much criticism about anonymous class before it went into voting phase, do people seriously reject a feature just because they either dont understand how it works or that they dont find it useful 'cause they are never gonna use it? How did goto get past the voting phase?

@krakjoe
Copy link
Member Author

krakjoe commented Oct 11, 2013

We have a vote because it's difficult to extrapolate an opinion or consensus from mailing lists ... there's not much can be done, the community has spoken ... apparently ...

@HallofFamer
Copy link

I see, guess you dont really know why they are opposing it? The only possible reason I think of is that they dont understand useful it is. I dont use generators or finally, but if I have a chance Id still vote yes for them both since I can see how they help certain developers to achieve their goals. The fact that you dont find something beneficial or that it does not make sense to you does not dictate that the other people feel the same way, Unfortunately, some people refuse to understand that... sigh

And if I may ask, would you turn this into a PECL extension if it gets rejected and continue to develop the inner class? Will you continue to work on it at times so that it will be compatible with PHP 5.6, 5.7 and onwards?

@robbytaylor
Copy link

How strange. I wouldn't have imagined that there could be a problem with a feature which encourages people to use objects more.

@krakjoe
Copy link
Member Author

krakjoe commented Oct 12, 2013

@HallofFamer not possible to do language changes in an extension, you must be able to patch parser ... so no ... pretty much end of the road ... maybe it can be brought up again later on but there's not, realistically, much point ...

@adrienbrault
Copy link

Why do people vote against this thing but vote for https://wiki.php.net/rfc/variadics ... wtf

@HallofFamer
Copy link

I see, thats too bad. I do hope you consider at least make it compatible with each major PHP releases beyond the next version(PHP 5.6, 5.7 etc). There are other features like operator overloading, which is available on PECL and got updated when PHP 5.3, 5.4 and then 5.5 was released.

@adrienbrault:
I guess some people are just stubborn or bored. I think the funnest thing you ever hear on PHP internals is a feature must 'fit the nature of PHP'. I was like, what is the nature of PHP? Does it have a nature? lol.

But seriously, variadics is a useful feature, like 'finally'. But just like 'finally', its a minor feature and PHP 5.6 is going to be a joke if variadics is labeled as its greatest new feature implemented. In PHP 5.3 we get namespace, and then PHP 5.4 with trait and PHP 5.5 with generator. Anonymous Class/Inner Class can make PHP 5.6 on par with PHP 5.3 with namespace, unfortunately we get so many stupid people saying no. sigh

@krakjoe
Copy link
Member Author

krakjoe commented Oct 13, 2013

@HallofFamer operator is different, it doesn't require a syntax change (thought I believe the first versions did come with a patch for some features), you can implement operators from an extension just fine ... by the way it was updated because someone happened to come along and ask why it didn't work or compile anymore, despite the fact that operator overloading is clearly useful and can be implemented as part of Zend or self contained in an extension using the APIs that Zend exposes; there is no prolonged effort from within the community to maintain the functionality of that, or nearly any other extension on pecl ...

This cannot be reasonably maintained outside of php, nor can inner classes, they both require patches to the parser, there is no reasonable, compatible way to self contain this functionality in an extension, if there were I probably would have saved myself the headache and just done that in the first place ...

@adrienbrault you should not try to work out why people act the way they do, there's no evidence at all that our behaviour is logical, or driven by logical concerns, and the only means you have to work it out with is logic, so it cannot be done ...

@krakjoe krakjoe closed this Oct 14, 2013
@prolic
Copy link

prolic commented Oct 19, 2013

I could cry that this is not accepted :-/

@HallofFamer
Copy link

It cant be helped, PHP internals choose to be stupid and stubborn, now they are rejecting extended keyword support too. sigh Perhaps all they wish for the next version of PHP is a purely procedural amateurish script language like PHP 3.0 lol.

@craigjbass
Copy link

I can't believe this didn't pass the vote. I thought we were supposed to be moving towards a better OO model inside PHP?

@craigjbass
Copy link

Instead focussing on minor concerns like UTF support. sigh

@HallofFamer
Copy link

Well UTF support is important, its nice they work on it. This should not rule out anonymous/inner class though in any way. According to what has been revealed so far, the PHP internals do not believe it is useful in this language. I can definitely find some usage myself, but apparently it aint enough to convince them.

Also some people speculate that that fact that anonymous class support is too java-like may be one important reason why it was rejected, since everything must fit in PHP's nature. I dont understand though, does PHP have a nature lol? And does it need one?

Anyway I just hope this project is maintained at least like some PECL packages that get updated every now and then. When PHP 5.6 officially comes out, I am not sure there will even be a feasible way to install this without breaking the other core functionality. sigh

@craigjbass
Copy link

PHP's nature is to be blind to OO design patterns. Personally I think we should be moving towards a Non-BC break PHP with nicer OO and the ability to choose to use types if you need them.

Multipurpose, and helpful. Instead of backward and confused.

Still. There has been some progress.

@HallofFamer
Copy link

umm I wonder though, if I have a custom build PHP and I want to add anonymous class support, do I only have to look for the file changes in this pull request and make these changes myself in the source C code? Or are there something else that needs to be modified, especially to make this work with later PHP versions?

@reeze
Copy link
Contributor

reeze commented Feb 13, 2015

Yes, but this patch are againt a certain PHP version, it might have difference.
But this is highly not encouraged, it will be hard to maintain, you have to merge upstream to your fork

@HallofFamer
Copy link

@reeze
Thanks, I understand the risk of course. I plan to port it to PHP 7 after it is released, and maintain a custom build PHP myself. sounds tricky, but I hope it will work, or at least @krakjoe will offer some advice if I run into issues incorporating this into PHP 7.

@reeze
Copy link
Contributor

reeze commented Feb 13, 2015

if you need help, you can contact me too :)

@HallofFamer
Copy link

@reeze :

Thank you so much, I very appreciate this.

@HallofFamer
Copy link

@reeze:
umm it seems that it does not work with PHP 5.6.5 if I just manually edit the C source code. I am getting these error messages after including anonymous class in php compile test:

FAILED TEST SUMMARY

declare bare anonymous class [tests/anon/001.phpt]
declare anonymous class extending another [tests/anon/002.phpt]
reusing anonymous classes [tests/anon/003.phpt]
testing anonymous inheritance [tests/anon/004.phpt]
testing reusing anons that implement an interface [tests/anon/005.phpt]
testing anons inside namespaces [tests/anon/006.phpt]
testing anons in functions outside of classes in namespaces [tests/anon/007.phpt]

And of course, using anonymous class in an actual php file on apache server will fail, throwing T_CLASS kind of error. Did I do anything wrong? The changes seems to be perfectly compatible with PHP 5.5 or 5.6, but it does not seem to work for me... Do you have any ideas how I can get this to work? Thanks.

@reeze
Copy link
Contributor

reeze commented Feb 17, 2015

Hi I am on holiday not,I will check it out maybe several days later:)

On 17 February 2015 at 14:43, HallofFamer [email protected] wrote:

@reeze https://github.com/reeze:
umm it seems that it does not work with PHP 5.6.5 if I just manually edit
the C source code. I am getting these error messages after including
anonymous class in php compile test:
FAILED TEST SUMMARY

declare bare anonymous class [tests/anon/001.phpt]
declare anonymous class extending another [tests/anon/002.phpt]
reusing anonymous classes [tests/anon/003.phpt]
testing anonymous inheritance [tests/anon/004.phpt]
testing reusing anons that implement an interface [tests/anon/005.phpt]
testing anons inside namespaces [tests/anon/006.phpt]
testing anons in functions outside of classes in namespaces
[tests/anon/007.phpt]

And of course, using anonymous class in an actual php file on apache
server will fail, throwing T_CLASS kind of error. Did I do anything wrong?
The changes seems to be perfectly compatible with PHP 5.5 or 5.6, but it
does not seem to work for me... Do you have any ideas how I can get this to
work? Thanks.

Reply to this email directly or view it on GitHub
#470 (comment).

Reeze Xia
http://reeze.cn

@HallofFamer
Copy link

@reeze :

No problem, thank you. Maybe I can get it to work before you come back, I hope for the best. And enjoy your vacation!

@reeze
Copy link
Contributor

reeze commented Feb 28, 2015

@HalloFamer anything I could do? :)

@HallofFamer
Copy link

@reeze: Sure, I'd like to see what was wrong with the modifications I made to a customized PHP 5.6.5 with anonymous class. It seems that all tests for anonymous class have failed, and I suspect the issue is more complicated. At this moment, would you mind trying to compile it on your machine and run the tests to see if it fails the tests too? And if so, I will think about what we can do next. I've uploaded the PHP 5.6.5 with anonymous class support below, thanks.
http://www.mediafire.com/download/a2xtf6pjs3wnp3f/php-5.6.5.tar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.