Git Product home page Git Product logo

Comments (9)

Rican7 avatar Rican7 commented on May 18, 2024

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.

gbouthenot avatar gbouthenot commented on May 18, 2024

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.

Rican7 avatar Rican7 commented on May 18, 2024

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.

gbouthenot avatar gbouthenot commented on May 18, 2024

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.

Rican7 avatar Rican7 commented on May 18, 2024

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.

gbouthenot avatar gbouthenot commented on May 18, 2024

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.

Rican7 avatar Rican7 commented on May 18, 2024

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.

chriso avatar chriso commented on May 18, 2024

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.

Rican7 avatar Rican7 commented on May 18, 2024

@chriso Thanks Chris!

from klein.php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.