Comments (9)
Correct. The reason for this decision was simply that it felt a little dirty to be adding class-scoped methods at runtime.
The new design direction of Klein V2 was to move towards a more OOP style, in which you could easily add custom methods through dependency injection. For example:
Create a custom Response class that extends Klein's:
<?php
class CustomResponse extends \Klein\Response
{
public function myCustomFunction($arg)
{
echo 'CUSTOM METHOD!';
echo 'You passed: '. $arg;
}
}
And then inject it into the dispatcher:
<?php
require 'vendor/autoload.php';
$k = new \Klein\Klein();
$k->respond('/', function($rq, $rs, $sr, $ap) {
$rs->myCustomFunction('woot!');
});
$rq = \Klein\Request::createFromGlobals();
$rq->server()->set('REQUEST_URI', '/');
$rs = new CustomResponse();
$k->dispatch($rq, $rs);
Does that make sense?
from klein.php.
Thank you for your wuick answer !
This is relevant, but this raise new problems related to multiple inheritance. Traits would probably be a better solution I think, but the're not supported with php 5.3.
I still find the kleinV1 syntax way more concise and elegant. I will try to find new ways to work around this limitation in kleinV1.
from klein.php.
No problem!
but this raise new problems related to multiple inheritance
How so? Multiple inheritance is a problem when trying to extend from multiple classes horizontally. Its no problem in a vertical chain. Simply extending a class to "extend" its functionality is a very common practice in PHP and all other object oriented languages. 👍
Traits would probably be a better solution I think, but the're not supported with php 5.3.
Traits are designed for a more horizontal inheritance pattern. They aren't required for this type of functionality extension.
From the PHP docs:
"A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance."
I still find the kleinV1 syntax way more concise and elegant
Klein V1 was beautiful! It was elegant, simple, etc. Unfortunately, though, it used a lot of magic to make PHP classes/objects behave in a way that allowed for hard to document/maintain code. This new, more explicit approach is an attempt to fix that behavior and build off of the direction that Klein was already heading in version 1 (new classes were added just to create headers, etc).
from klein.php.
In your example, you use
class CustomResponse extends \Klein\Response
If there are more than one source of method defining, you'll get another
class AnothercustomResponse extends \Klein\Response
// (or)
class AnothercustomResponse extends CustomResponse
Anyway, I am not satisfied with this solution, because I want to be able to have many method injections.
I will soon send a pull request to restore this behaviour. Hope you will accept it.
(due in a few hours)
from klein.php.
What do you think about this, @chriso?
I think its providing awkward magic and fights the design principles of the object oriented structure. Even Sinatra (kleins original inspiration) uses an object oriented class inheritance model for extending Sinatra's functionality (through helpers), and Sinatra's a more "functional" (a little less OOP structured in use, not design) micro-framework.
Also, I still don't get what you're trying to say here, @gbouthenot. Extending classes to add OO functionality is the most logical and "classical" way of achieving your goal here. Your solution would make more sense in a "prototypical" inheritance model.
from klein.php.
I see your point. What I am trying to say is that kleinV2 broke this feature I used a lot. I think that the V2 should not purposely remove features, when the changes to get the features back are minimal.
from klein.php.
Yea, I understand that, but V2 wasn't meant to be backwards compatible, honestly. Its a totally different control flow, methods were moved and renamed, its no longer in global scope, etc.
Also, I originally had a similar magic __call()
method in Klein V2, but removed it per @chriso's request (see 14).
from klein.php.
Klein v1 opted for mixins rather than OO. This is great if you want to get an app up and running quickly (one of my original goals - I just needed to bang out admin scripts quickly) but you'll eventually run into trouble. What if a user wants to override or extend one of the built-in request/response methods? Inheritance is the way to go.
@gbouthenot all of your klein v1 mixins are dispatched (globally) to each route so I'm not sure why multiple inheritance was brought up.
@Rican7 my vote is to keep the magic to a minimum and to not worry about backwards compatibility with v2.
from klein.php.
@chriso Thanks Chris!
from klein.php.
Related Issues (20)
- Broken link in README
- JSON pretty print HOT 1
- PHP warning array_flip()
- Multiple instances? HOT 1
- How could we use DI with this package HOT 1
- subroute not working HOT 5
- Klein dispatch() LockedResponseException HOT 2
- $this->PageTitle is not working HOT 2
- is it possible to create methods with parameters in $app->register HOT 1
- Question, handling a client area HOT 2
- Headers not sent if buffer length exceedes value defined in ini.output_buffering
- More concise, Laravel-style Controller@method callbacks HOT 3
- Error with SVG images HOT 2
- Support apcu without apcu_bc?
- Central callback/hook before route response is called HOT 1
- is this package still maintained? HOT 4
- Access "pathname()" method in view using $this->pathname()
- Question: how use parameter in route?
- Add more better regex's
- Second Subdirectory Routing Not Working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from klein.php.