-1

Actually i am new to OOPS concepts and it is hard to understand, and i also read it somewhere that "we do not have overloading in PHP".I am studying this example but it didnt get me somewhere.

<?php
class Toys{
    private $str;
    public function __set($name,$value){
        $this->str[$name] = $value;
    }

    public function __get($name){
        echo "Overloaded Property name = " . $this->str[$name] . "<br/>";
    }

    public function __isset($name){
        if(isset($this->str[$name])){
            echo "Property \$$name is set.<br/>";       
        } else {
            echo "Property \$$name is not set.<br/>";
        }
    }

    public function __unset($name){
        unset($this->str[$name]);
        echo "\$$name is unset <br/>";
    }
}

$objToys = new Toys;

/* setters and getters on dynamic properties */
$objToys->overloaded_property = "new";
echo $objToys->overloaded_property . "\n\n";
/*Operations with dynamic properties values*/

isset($objToys->overloaded_property);
unset($objToys->overloaded_property);
isset($objToys->overloaded_property);
?>
Harshal Bhavsar
  • 1,598
  • 15
  • 35
mickey
  • 101
  • 1
  • 3
  • 13

3 Answers3

1

Method Overriding

Method Overloading

Simple Explanation

Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.

Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.

Wearybands
  • 2,438
  • 8
  • 34
  • 53
-1

Overloading allows you to write function with the same name but a different type of arguments. The compiler selects the right function as compile time, based on the types of the arguments.

PHP does not have it because it has no static types (at compile time, the types are not known).

Overriding only is exists in OO. Overriding refers to defining a mehtod in a class, that a parent class has already defined. So your classes 'overrides' method that was already defined by a parent.

Jan Groothuijse
  • 320
  • 1
  • 3
  • For the basics, you are correct. I don't understand the down-vote without comments .. but first of all a method accepts an argument (execution), but it is defined by a parameter (definition). The argument is the value a parameter is given. Do not confuse the two. – dbf Sep 26 '16 at 12:22
-2

The parameter is an Object so you can add values to this Object. So you dont need overloading, because you can pass as many values as you want.

crakxx
  • 3
  • 2