Giter Club home page Giter Club logo

Comments (6)

spawnia avatar spawnia commented on August 29, 2024

Yeah, interfaces are not supported yet. I will probably add support as soon as I have a use case for them. If you want to contribute, I am open to reviewing and aiding in the implementation of this feature.

from sailor.

simPod avatar simPod commented on August 29, 2024

Hmm. So what if we add methods to generated classes that represent Object types and let them implement interfaces representing Interface types for shared fields?

from sailor.

spawnia avatar spawnia commented on August 29, 2024

The generated result objects allow access to fields in the form of properties. I think this is more elegant than methods because:

  • it allows null-safe access with ?? (this will be less imporant with PHP 8 null safe method calls ?->)
  • no need to type an extra set of parantheses
  • easy setup of mocked results for test by just setting the properties

That said, interfaces without methods still provide value:

  • while they cannot define properties, they can indeed define virtual properties via @property. We will have to check how this interacts when the implementing object type narrows a field type, e.g. changing it to be non-nullable
  • it can be used as a strong type hint

from sailor.

simPod avatar simPod commented on August 29, 2024

I see the PR, just few notes on those interfaces.

easy setup of mocked results for test by just setting the properties

methods would read properties so the same applies, no?

it allows null-safe access with ??

method calls do so? or I don't understand


Implemented interfaces only with properties seem a bit dirty as it is not how the language was designed

from sailor.

spawnia avatar spawnia commented on August 29, 2024

Given this code:

class Foo {
    public ?Foo $bar;
    function bar(): ?Foo { return null; }
}

$foo = new Foo;

The following will work without error:

$foo->bar->bar ?? null;

This will throw with Fatal error: Uncaught Error: Call to undefined function bar():

$foo->bar()->bar() ?? null;

PHP 8 would allow to do it safely and succintly, but even then properties are more terse:

$foo->bar()?->bar();

from sailor.

simPod avatar simPod commented on August 29, 2024

Ah ok I see what you meant by that. I go with PHP 8 everywhere.

from sailor.

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.