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

Allow returning an arbitrary object from the mocked "new" call #11

Closed
wladekb opened this issue Mar 3, 2015 · 4 comments
Closed

Allow returning an arbitrary object from the mocked "new" call #11

wladekb opened this issue Mar 3, 2015 · 4 comments

Comments

@wladekb
Copy link

wladekb commented Mar 3, 2015

Feature request:

  • allow ZEND_NEW to be overloaded in such a way that it returns an arbitrary object coming from the overload callback (call to constructor is skipped unconditionally in such case)

I'd be happy to hear back if that feature fits the general roadmap of uopz.

Background story:

Wikia currently uses PHP 5.4 and with help of runkit + modified version of php-test-helpers (Wikia/php-test-helpers@9a28725) we were able to have a robust testing environment that satisfies our needs in the area of testing MediaWiki-derived code base.
As we're transitioning to PHP 5.6 we found out that uopz gives us all the functionality of the previous tandem in a single extension except the feature described above. We'd be very happy if we could get the missing piece implemented.

@wladekb wladekb changed the title Allow returning an arbitrary value from the mocked "new" call Allow returning an arbitrary object from the mocked "new" call Mar 3, 2015
@krakjoe krakjoe closed this as completed in 28e09af Mar 3, 2015
@krakjoe
Copy link
Owner

krakjoe commented Mar 3, 2015

Done.

Instead of returning, accept the class by reference and set that to the object, like this:

<?php
class Other {}

uopz_overload(ZEND_NEW, function(&$class){
    switch ($class) { # beware recursion !
        case "stdClass":
            $class = new Other();
        break;
    }
});

$object = new stdClass();

var_dump($object);
?>
krakjoe@fiji:/opt/src/uopz$ php -duopz.overloads=1 uopz.php
object(Other)#2 (0) {
}

@wladekb
Copy link
Author

wladekb commented Mar 3, 2015

It's great to see such a quick response and code change. Thanks a lot!

@krakjoe
Copy link
Owner

krakjoe commented Mar 4, 2015

You're welcome :)

@TheCelavi
Copy link

Hello, is there any plan to support this somehow again in PHP 7.x version?

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

No branches or pull requests

3 participants