20
rant1ng
5y

I think I just blew my own mind here.

Look at this:

Class SomeClass
{
_call($functionName, $arguments)
{
return call_user_func(array('SomeClass','uselessMethod'), 'method');
}

method($foo)
{
return new Adapter($foo)->execute($this);
}

uselessMethod()
{
return $this->method(__FUNCTION__);

}

}
so __FUNCTION__ resolves to
Caller:

You can run that code, whether you comment out uselessMethod, or not.

Adapter is a function that looks for what class to call depending on a database value
and execute the call.

So api basically uses a chain call to do stuff like this in controllers, here's how
I call the above:

$someObject = (new Class($object))->uselessMethod()->doSomething()->doSomethingElse();

But like, eventually my code matured to where all those methods in the chain call have the same one line return that calls my adapter to find the logic to run.

So, basically, I can now have a class with headless function calls that calls a directory of other classes, that are all defined in a contract somewhere. So as long as those classes
all adhere to the contract, it will never return an error.

I can't think of any reason to do this, other than my setup, and I have a sneaky feeling,
as dirty as this trick is, that there's a bad reason my code has come to being able to do this.

Maybe wrong strategy pattern from the beginning?

I'm sure it'll come to me like 3 days from now..

Comments
  • 7
    I'm amazed and disgusted about what reflection can do...
  • 3
    I have only found a few usecases where dynamic function calls are the proper choice. This is not one of them!

    (And even when it is the proper choice, I still feel conflicted about it.)
  • 3
    @Root

    Its a program that interacts with 100 websites.
    There is a lot of logic overlap but not enough
    that I can create all encompassing solutions.

    The 100 website classes extend an abstract class. So now to get to the right logic I need
    is to call SomeWebsite.php which easily matches with SomeWebsite.com right.

    If default abstract doesn't work, and I need
    tod ebug I can just ctrl+p
    and type the website name and the class'll come up in my editor. I can write the logic
    and it's done.

    So I literally have 100 class files, 80 of which are empty, just so I could do this.

    If any of them change things or get cute, I can
    very quickly edit the logic in SomeWebsite.php even though there's a ton of files.

    Fluent api wrapper is nowt re-routed as no
    work need by wrapper anymore. Still needs to
    be in place, thoug since its called from everywhere.
Add Comment