Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,167,242 members, 7,867,634 topics. Date: Friday, 21 June 2024 at 08:17 PM

Chaining In Php - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Chaining In Php (749 Views)

A Beginners Guide To Object-oriented Programming (OOP) In PHP 5+ / Ecommerce Web Application Written From Scratch In Php For Sale With Mobile View / What's The Difference Between "require Once" And "include" In Php? (2) (3) (4)

(1) (Reply)

Chaining In Php by cdeveloper(m): 2:58pm On Mar 09, 2010
I have been a fan of the ability to achieve chaining in Javascript as demonstrated by the famous javascript library jQuery with its philosophy of Code less Do More after grappling around this idea for a while i started thinking about the possibility of bringing it to php and somehow i have seen that even php has that ability to achieve chaining too but i am yet to explore the possible application of this concept. though one area i am looking at is web services.This is how i was able to do it. I may have to look for a more viable application of this


/* Sample beta class */

class Beta
{
  private $firstname;
  private $surname;
  private $aga;

public function __construct($age=0)
{
   $this->firstname='Cdeveloper';
   $this->surname='Ama';
   $this->age=$age;
}

/* A method to set a new name */
public function setFirstname($fname='')
{
     $this->firstname=$fname;
     return $this;
}
/* A method to set surname */
public function setSurname($sname='')
{
   $this->surname=$sname;
   return $this;
}
/* a method to set a new age*/
public function setAge($age=0)
{
    $this->age=$age;
    return $this;
}
/* a method to get fullname */
public function getName()
{
  return ucfirst(strtolower($this->firstname))." ".ucfirst(strtolower($this->surname));
}

}

Now to see the chaining thing in action i will create an instance of the class

$beta=new Beta(20);
//get the current default fulllname
echo $beta->getName() ;
Output: Cdeveloper Ama
//to set a new name and output it
echo $beta->setFirstname("Dummy"wink->setSurname("Don"wink->getName();
Output: Dummy Don
/* to set firstname, age,surname and out put them*/
echo $beta->setFirstname("Dummy"wink->setSurname("Don"wink->setAge(30)->getName();



This is really funny but a viable idea that can be used in building api that people can use much in the same way that jQuery is used.
The secret is in returning $this in the setters methods. This variable is internally an instance of the class itself and when you return it from
the method it allows you to still call other methods associated with the object. This is one place that Javascript is king,and this is the concept
that makes jQuery standout. with one line of code you can do more
House what do you guys make of this?

(1) (Reply)

Please Review This Site / Trudigits Webhosting - Easter Promo 50% Off / Logo Designer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.