Overloading

  • Overloading in PHP provides means to dynamically create properties and methods.
  • These dynamic entities are processed via magic methods, one can establish in a class for various action types.
  • All overloading methods must be defined as Public.
  • After creating object for a class, we can access set of entities that are properties or methods not defined within the scope of the class.
  • Such entities are said to be overloaded properties or methods, and the process is called as overloading.
  • For working with these overloaded properties or functions, PHP magic methods are used.
  • Most of the magic methods will be triggered in object context except __callStatic() method which is used in static context.
OVERLOADING

Property overloading

  • PHP property overloading allows us to create dynamic properties in object context.
  • For creating those properties no separate line of code is needed.
  • A property which is associated with class instance, and not declared within the scope of the class, is considered as overloaded property.

Some of the magic methods which is useful for property overloading.

  • __set(): It is triggered while initializing overloaded properties.
  • __get(): It is utilized for reading data from inaccessible Properties.
  • __isset(): This magic method is invoked when we check overloaded properties with isset() function.
  • __unset(): This function will be invoked on using PHP unset() for overloaded properties.

Kotlin Operator Overloading

In this article, you will learn about operator overloading (define how operator works for user defined types like objects) with the help of examples.

When you use operator in Kotlin, it’s corresponding member function is called. For example, expression a+b transforms to a.plus(b) under the hood.

fun main(args: Array<String>) {
    val a = 5
    val b = 10

    print(a.plus(b)) // print(a+b)
}

When you run the program, the output will be:

15

In fact, plus() function is overloaded to work with various Kotlin basic types and String.

// + operator for basic types
operator fun plus(other: Byte): Int
operator fun plus(other: Short): Int
operator fun plus(other: Int): Int
operator fun plus(other: Long): Long
operator fun plus(other: Float): Float
operator fun plus(other: Double): Double

// for string concatenation
operator fun String?.plus(other: Any?): String

You can also define how operator works for objects by overloading its corresponding function. For example, you need define how + operator works for objects by overloading plus() function.

Example: Overloading + Operator

fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)

    var sum = Point()
    sum = p1 + p2

    println("sum = (${sum.x}, ${sum.y})")
}

class Point(val x: Int = 0, val y: Int = 10) {

    // overloading plus function
    operator fun plus(p: Point) : Point {
        return Point(x + p.x, y + p.y)
    }
}

When you run the program, the output will be:

sum = (5, 1)

Here, the plus() function is marked with operator keyword to tell compiler that + operator is being overloaded.

The expression p1 + p2 is transformed to p1.plus(p2) under the hood.


Example: — Operator Overloading

In this example, you will learn to overload -- operator. The expression --a is transformed to a.dec() under the hood.

The dec() member function doesn’t take any arguments.

fun main(args: Array<String>) {
    var point = Point(3, -8)
    --point

    println("point = (${point.x}, ${point.y})")
}

class Point(var x: Int = 0, var y: Int = 10) {
    operator fun dec() = Point(--x, --y)
}

When you run the program, the ouput will be:

point = (2, -9)

Remember that,

operator fun dec() = Point(--x, --y)

is equivalent to

operator fun dec(): Point {
    return Point(--x, --y)
}

Few Important Points

1. When you overload operators, you should try to maintain the original spirit of the operator. For example,

fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)

    var sum = Point()
    sum = p1 + p2

    println("sum = (${sum.x}, ${sum.y})")
}

class Point(val x: Int = 0, val y: Int = 10) {

    // overloading plus function
    operator fun plus(p: Point) = Point(x - p.x, y - p.y)
}

Although the program above is technically correct, we have used + operator to subtract corresponding properties of two objects which made the program confusing.

2. Unlike languages like Scala, only a specific set of operators can be overloaded in Kotlin. Visit the page to learn about operators that can be overloaded in Kotlin and their corresponding member functions.