0

Possible Duplicate:
Why does PHP 5.2+ disallow abstract static class methods?
Why can't you call abstract functions from abstract classes in PHP?

I'm running this code on PHP 5.3.8:

abstract class Geometry
{
    abstract public static function fromArray(array $array);
}

class Point extends Geometry
{
    public static function fromArray(array $point)
    {
        return new self($point[0], $point[1]);
    }
}

And receive the following error:

Strict Standards: Static function Geometry::fromArray() should not be abstract

  • What's wrong with this approach?
  • Any viable alternative to force concrete classes to implement this factory method?
Community
  • 1
  • 1
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • @ajreal: `return new static` doesn't address the issue, and is not the desired behavior (I want to return a new `Point`, not a new `Geometry`) – BenMorel Dec 05 '11 at 18:31
  • @ajreal: similar issue, but I don't face the same use case and my code doesn't trigger the *fatal error* this user faces. My request is specifically targeted at the Strict Standards warning. – BenMorel Dec 05 '11 at 18:34
  • return without the `new` – ajreal Dec 05 '11 at 18:35
  • @ajreal: `return static($point[0], $point[1])` => Parse error: syntax error, unexpected '(', expecting T_PAAMAYIM_NEKUDOTAYIM – BenMorel Dec 05 '11 at 18:41
  • @rdlowrey: same question, but different goal: we can reasonably argue that he "should redesign things so getSelectSQL() is an abstract /instance/ method" as a commentator says, however my aim is to force a concrete class to provide a **factory method**, which is by definition, **static**. So I'd be happy to have an answer at least to my second question: "any viable alternative to force concrete classes to implement this factory method?" – BenMorel Dec 05 '11 at 19:13
  • Interesting reading: https://bugs.php.net/bug.php?id=53081 – BenMorel Dec 05 '11 at 19:35

1 Answers1

1

Could you make Geometry an interface, and have Point implement it?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • My goal was to implement the [OpenGIS model](http://dev.mysql.com/doc/refman/5.0/en/opengis-geometry-model.html), and I do have some common behavior in the `Geometry` class, thus I have to introduce an interface just to remove the Strict Standards error: `class Geometry implements IGeometry` (even though the code worked fine if we ignore the warning). That said, I think your solution is the only one I've left! – BenMorel Dec 06 '11 at 09:46
  • Glad that works for you. I'm not such an expert as to be certain this is a deliberate behaviour - would it be worth trying on 5.4 to see if the same warning results? – halfer Dec 06 '11 at 11:00
  • *very* interesting indeed, I just tried with the latest build of PHP (5.4.0RC3-dev), and no E_STRICT error is raised for this code! Did they make another 180° turn? – BenMorel Dec 06 '11 at 12:37
  • If it isn't covered by the ticket above, suggest you raise another. And require PHP 5.4+ for your application ;-) – halfer Dec 06 '11 at 12:50