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

Error on call $service->helper_function() #131

Closed
rafas opened this issue Aug 10, 2013 · 6 comments
Closed

Error on call $service->helper_function() #131

rafas opened this issue Aug 10, 2013 · 6 comments

Comments

@rafas
Copy link

rafas commented Aug 10, 2013

I'm trying to create a help function in class $service.
$service->test = function ($str) {
return htmlentities($str);
};

And call with the same parameter:
$service->test('texto');
or with $this (in render file)
$this->test('texto');

But it returns an error.
Fatal error: Call to undefined method Klein\ServiceProvider::test() in C:\htdocs\Labs\index.php on line 25

The function __call does not exist in the class Klein\ServiceProvider.
I think the old version of PHP have enough function __get but now it seems that it is mandatory to use the __call.

@rafas
Copy link
Author

rafas commented Aug 10, 2013

I tried to fix, and managed to get some result with the code below, inserting it in class Klein\ServiceProvider (after the function __get).

public function __call($key,$param)
{
return call_user_func_array( $this->shared_data->get($key) , $param );
}

Please, whoever knows how to contribute to the project by sending revisions, upgrade to have this solution.

@Rican7
Copy link
Member

Rican7 commented Aug 16, 2013

@rafaelwidenet The lack of a __call() implementation in the ServiceProvider class was intentional. This related thread might give you some insight as to why.

@rafas
Copy link
Author

rafas commented Aug 22, 2013

@Rican7
See the code "Example 4 - Sending objects / files", published on the homepage of the project.
With this decision, do not keep the magic function "__call", this example should be extinguished.

I honestly really like this example, and although it does not seem "politically correct" represents very well the dynamics of the class.

I'm currently using this class in a modular system, which creates fast routines for repeated calls. This is not a "magnificent and perfect way to create a project", but given the cost of agility, is what we are doing.

Anyway ... I am seriously considering stopping the future use of this class (updated) to allow the creation of customized routines based functions.

Is there any alternative way to use the fast creation of functions?
I tried with $app->register(), but it calls the function only once (as expected) for variable initialization.

Thank you for your attention.

@Rican7
Copy link
Member

Rican7 commented Aug 26, 2013

@rafaelwidenet Ah, Example 4 just needs to be switched from the service to the response as the caller.

Is there a reason why you don't want to just extend the Request, Response, or ServiceProvider classes to add your own methods? Class extension is a much more efficient way of running code (the classes can be op-code cached) versus defining mixin-like methods at runtime.

For example, let's add a new method to the Response class:

class CustomResponse extends \Klein\Response {
    public function respondLoudly() {
        $this->append('!!!!!');
        $this->send();
    }
}

And in our app, when dispatching...

// Pass in a new instance of our custom Response object
$klein->dispatch(null, new CustomResponse());

@rafas
Copy link
Author

rafas commented Sep 3, 2013

Hello @Rican7

There is no exact reason for me not to want to extend classes. I'm just not developing the application fully object-oriented. My intention is to use the library Klein to organize and keep safe a systematic programming, but without being "extremely OO".
The reason for begin the use of Klein is just the simplicity.

But okay, I found a reasonable solution.
Instead of defining callable $service->func_name = function () {}
I define a function in the variable $ app (not lazy)
$app->func_name = function () {}
As I'm not using these functions in "render", is not being missed.

Thanks.

@Rican7
Copy link
Member

Rican7 commented Sep 3, 2013

@rafaelwidenet
I'm glad that worked for you. :)
Closing...

@Rican7 Rican7 closed this as completed Sep 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants