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?
6
  • @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) Commented Dec 5, 2011 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. Commented Dec 5, 2011 at 18:34
  • return without the new Commented Dec 5, 2011 at 18:35
  • @ajreal: return static($point[0], $point[1]) => Parse error: syntax error, unexpected '(', expecting T_PAAMAYIM_NEKUDOTAYIM Commented Dec 5, 2011 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?" Commented Dec 5, 2011 at 19:13

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

My goal was to implement the OpenGIS model, 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!
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?
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?
If it isn't covered by the ticket above, suggest you raise another. And require PHP 5.4+ for your application ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.