Skip to content

Allow setting properties after instantiation #4

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

Merged

Conversation

sergeylukin
Copy link
Contributor

I find it more readable to assign properties/methods after instantiation,
like so:

$stub = new s();
$stub->foo = 'bar';
$stub->callMe = function() {};

This commit allows doing just that.

I find it more readable to assign properties/methods after instantiation,
like so:

```
$stub = new s();
$stub->foo = 'bar';
$stub->callMe = function() {};
```

This commit allows doing just that.
@nulpunkt
Copy link
Owner

nulpunkt commented Nov 6, 2014

If this is the behavior you want, a stdClass should cover your needs. I'm not totally against this, I'm just unsure of the use case.

The original reason I did this project, was because I was tired of writing methods, which just returned the same thing.

Are we starting to invent the anonymous classes from java here? :)

@sergeylukin
Copy link
Contributor Author

Assigning methods and properties after instantiation doesn't break any functionality, it just adds an option, which I find nicer than the original one in some cases.

As far as I know stdClass doesn't support directly callable methods, which makes it useless for cases when, for instance, a Model (which has methods), should be replaced with a stub. Please let me know if I'm wrong here.

@nulpunkt
Copy link
Owner

I stand corrected, just tried this out:

public function testStdObj()                                                                                                                                                                                                                   
{                                                                                                                                                                                                                                              
    $t = new stdClass;                                                                                                                                                                                                                         
    $t->lol = function () {                                                                                                                                                                                                                    
        return 'ttt';                                                                                                                                                                                                                          
    };                                                                                                                                                                                                                                         
    $this->assertSame('ttt', $t->lol());                                                                                                                                                                                                       
}

and it failed with PHP Fatal error: Call to undefined method stdClass::lol(), I was not expecting that.

Anyways, if you like this way better, why not support it.

nulpunkt added a commit that referenced this pull request Nov 10, 2014
…tiation

Allow setting properties after instantiation
@nulpunkt nulpunkt merged commit 88816a5 into nulpunkt:master Nov 10, 2014
@sergeylukin
Copy link
Contributor Author

Amazing. Thank you!

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.

2 participants