Changing return type when overidding a function in the subclass in PHP?

php override function in child class
php call parent method
types of inheritance in php with example
method overloading in php w3schools
php override protected function
php extend class from another file
php override function with different parameters
multilevel inheritance in php

Is this bad? Or this is very common in PHP framework? For example, in the parent class, there is a save() function which returns the number of rows affected in the database. Then in the child class, I override this function to do some pre-validation, and also would like to simply return a success/failed boolean value.

I agree with consensus that it's bad to change the type (or even the meaning) of the return value.

Here is an illustration how it will be bad:

Let say you have a function that accepts a string and a 'Writer':

function printThroughWriter(String $text, Writer $writer) {
    $count = $writer->write($text);
    print "Writer '".get_class($writer)."' printed $count characters.";
}

And here is the original 'Writer' class:

class Writer {
    public function write(String $text) {
        print $text;
        return strlen($text);
    }
}

What if we pass a 'LazyWriter' instead to the function:

class LazyWriter extends Writer {
    public function write(String $text) {
        return true;
    }
}

The assumption that the return value of the Writer::write() is the number of characters in printThroughWriter has been broken, thus causing inconsistency.

PHP's Method Overriding - Oceanize Geeks, class Writer { public function write(String $text) { print $text; return strlen($text); } } While there is nothing enforcing return types in PHP, it is a good idea to If the overridden method returned a boolean, the overriding method  Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. For example, when you extend a class, the subclass inherits all of the public and protected methods

Yes it is bad. It will be hard for the client of that method to be know that to expect. UNLESS, the method is designed to handle it (which in this case, it does not seems like).

I will strongly advice against it.

Object Inheritance - Manual, In overriding we can re-declare parent class method in child class. So, basically the purpose of overriding is to change the behavior of your parent class method. return "\n Parent - the parameter value is $param"; Overriding method can have different return type but this new type should be, A Non-Primitive. A Subclass of what base class’s overridden method is returning i.e. co-variant Return Type.

You should be overriding the underlying code, not the contract.

So, no, it's not good to change the return values (which is part of the contract).

PHP RFC: Return Type Declarations, For example, when you extend a class, the subclass inherits all of the public and not by the object) and that changing or adding variables in the parent ($e, $d) are not inherited by the child. Overriding a method which is called from base class works like this: return call_user_func_array(array($this, $method), $value​); The method overriding is a technique to achieve polymorphism. Sometimes, we want a subclass object to give different results for the same method when subclass object invokes it. This can be done by defining the same method again in subclass. The method has the same name, same arguments, and the same return type.

The idea of inheritance is to allow clients to use the sub types of a class interchangeably. If you are changing return types of sub types then you are destroying the normative utility of inheritance and will just end up creating confusion: "Here I take a type of Collection, but is myCollection.add() going to return me a boolean or the element that was added or something else? I have no idea! I can't do anything anymore with this type! I'm going to have to make a method for each sub type in order to handle this problem!"

Class Abstraction - Manual, Change language: For example, if the abstract method is defined as protected, the function implementation must be defined return type declaring not defined in abstract class, set here echo "myProduct overrides the defaultProductImplementation's doBuy() here {$this->_bought}". class subclass extends my_class { When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. The version of a method that is executed will be determined by the object that is used to invoke it.

While there is nothing enforcing return types in PHP, it is a good idea to remain consistent with overridden methods because to do otherwise means that you will increase coupling and decrease encapsulation.

Think of public methods and their return types in terms of outlets and appliances. Any appliance should be able to plug into any outlet and every outlet should output the same amount of energy to each appliance, unless of course the outlet has been specifically designed for a specific type of appliance. What would happen if someone decided that green outlets should output one amount of energy and red ones another. Suddenly you'd have to always be concerned with the specific class of outlet that your appliance is dealing with.

Designing public methods and properties is exactly the same and overridden methods and properties should always behave consistently regardless of the context in which they are accessed.

Traits - Manual, The precedence order is that methods from the current class override Trait Example #6 Changing Method Visibility __CLASS__ will return the name of the class in which the trait is being used. public static function setType($type){ When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the ways by which C# achieve Run Time Polymorphism(Dynamic Polymorphism).

PHPStan type system – polymorphism, type variance and method , That means a method of a subclass must have the same return type and in PHP are invariant, a subclass can not change types declared by a superclass. Currently, PHPStan allows to override a method with incompatible  PHP 5 introduces abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation. When

Introduction To Class Inheritance In Object Oriented PHP, If you are using return type declarations in PHP7, don't change the return type of a method when overriding it in a subclass or a fatal error will  Because parameter types in PHP are invariant, a subclass can not change types declared by a superclass. And, as mentioned before, PHP doesn't have real method overloading - ie. one class can't have more methods with the same name and different parameter type or a different number of parameters, as eg., a class in C# can.

Java Method Overriding Examples and Concepts: Overriding Rules , In java, a method can only be written in Subclass, not in same class. The return type should be the same or a subtype of the return type declared in the original  If you are using return type declarations in PHP7, don’t change the return type of a method when overriding it in a subclass or a fatal error will occur. PHP5 does not support return type declaration.