munchie(); // $this is defined (a) Peanut::munchie(); // $this is not defined. $b = new Pecan(); //new object of class Pecan $b->scoff(); // $this is defined (b) Pecan::scoff(); //$this is not defined. " />
//OOP - The Basics
//Class
//Just imagine an object, like a new generation of killer robot needing a template. We call this template a "Class". The class can be any name that is not a reserved word in PHP. Surrounding the curly braces we place the definition of the variables, methods and members. A pseudo-variable, $this is at our disposal when a method is called from within an object.
//It is a reference to the calling object.
class Peanut { function munchie() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")n"; } else { echo "$this is not defined.n"; } } }
class Pecan { function scoff() { Peanut::munchie(); } }
$a = new Peanut(); //new object of class Peanut
$a->munchie(); // $this is defined (a) Peanut::munchie(); // $this is not defined.
$b = new Pecan(); //new object of class Pecan
$b->scoff(); // $this is defined (b) Pecan::scoff(); //$this is not defined.