Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How this class and sub methods use works?

Tags:

oop

php

class

I have been browsing some php source code and need to know how the following class and sub methods use works:

<?php
$me = new Person;
$me->name("Franky")->surname("Chanyau")->phone("+22", "456 789");
?>

I have pretty solid knowledge of OOP so I don't want a 101. I just need to know how to make the above code possible.

like image 453
Franky Chanyau Avatar asked Sep 22 '10 07:09

Franky Chanyau


People also ask

What is a method and how they are used in a class?

A method in object-oriented programming is a procedure associated with a class. A method defines the behavior of the objects that are created from the class. Another way to say this is that a method is an action that an object is able to perform.

What is class and subclass?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

How do you call a method from a sub class?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.


1 Answers

Method chaining is possible, by

return $this;

at the end of the method.

Explained here: phpandstuff: Method Chaining Plus Magic Setters

These methods usually set an instance variable and then just return $this.

public function phone($param) {
  $this->phone = $param;
  return $this;
} 
like image 171
Tilman Koester Avatar answered Sep 29 '22 23:09

Tilman Koester