Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7906009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:43:34+00:00 2026-06-03T10:43:34+00:00

I’m testing my existing code on PHP5.4 prior to upgrade. I’ve discovered that the

  • 0

I’m testing my existing code on PHP5.4 prior to upgrade. I’ve discovered that the following code no longer works because PHP has tightened up its inheritance model. Due to this tightening, I’ve been reading about SOLID, and specifically Liskov’s substitution principle (I’m a self taught programmer) so that I can improve my code and not suffer from future “tightenings”.

interface IComparable {
    public function equals(self $other);
}

class A implements IComparable{
    protected $var;

    public function __construct($v){
        $this->var=$v;
    }

    public function equals(self $other){
        return ($this->var == $other->var) ? 'equal' : 'different';
    }
}

$a1= new A(7);
$a2= new A(5);
$a3= new A(5);

echo $a1->equals($a2),"\n";
echo $a2->equals($a3),"\n";

php 5.3 result:

  • different
  • equal

php 5.4 result:

PHP Fatal error: Declaration of A::equals() must be compatible with
IComparable::equals(IComparable $other)

I can avoid the php5.4 error if I write the code this way:

interface IComparable {
    public function equals($other);
}

class A implements IComparable{
    protected $var;

    public function __construct($v){
        $this->var=$v;
    }

    public function equals($other){
        if(get_class($other) != get_class($this)) return false;
        return ($this->var == $other->var) ? 'equal' : 'different';
    }
}

but does the fix comply with Liskov’s substitution principle since the function obviously won’t accept any argument type? And if it doesn’t, how can I code an inheritable function that will do what I need –compare 2 objects of the same type– and comply with good OOD principles?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:43:35+00:00Added an answer on June 3, 2026 at 10:43 am

    First of all: the PHP 5.3 behavior is a bug, so you can’t use it as a yardstick to judge any other approach.

    Going forward, the LSP was already violated in your 5.3 version code. Consider:

    interface IComparable {
        public function equals(self $other);
    }
    

    This says that “any IComparable can compare itself to any other IComparable” (the semantics of the comparison are not important for the discussion). The class A then proceeds to violate the LSP because it does not support comparison with any IComparable — just with those that happen to be A instances.

    The PHP 5.4 version does not violate the LSP because the new version of IComparable says “I can compare myself to any other object” and that’s exactly what class A does as well.

    If your intent was to keep the 5.3 version contract, then IComparable should read

    interface IComparable {
        public function equals(IComparable $other);
    }
    

    and class A would of course use the same signature. This would not violate the LSP and it would work correctly across both versions.

    If your intent was to declare “an IComparable instance can compare itself with instances of the same type, whatever that is”, then you are not in luck because this contract cannot be expressed using method signatures and type hints.

    Update: Turns out your intent was to declare “instances of this class can compare themselves” and IComparable was intended simply to force you not to forget to do this.

    In this case, the solution would be simply to forget about IComparable and use self in the signature of A::compare(). You do lose the compiler’s forcing you to remember to define the requisite method, but that’s IMHO a minor issue (especially because the interface only declares one method).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.