Php Class Examples Define abstract class abstract class demo Using Abstract Classes and Methods Using Abstract Classes in PHP 5 abstract keyword is used to say that a method or class cannot be created in your program as it stands Class Constant Define class constant Define class level constant value to control the class behaviour A Class with a Constructor A Basic PHP 4 Class A Basic PHP 5 Class A class is a collection of variables and functions working with these variables. Aggregating an address object Bird class Basic Object Accessing Class Type Hints Implementing a Simple Class Person class PHP class declaration structure book class Pre-defined methods Using an aggregated class Empty class Inheriting a Shape Class Improved InheritanceCreate two child classes Creating a Class That Inherits from Another Class inheritance in action Create Class instance Pass class instance as parameter Call class methods directly Accessing the Attributes of a Class by Using Functions A Class with a Method Accessing a Property from Within a Method Access properties Calling an Overridden Method (PHP 5 Syntax) Class Member Overloading Class Member and Method Definitions Defining three member functions for Cat Overriding Methods The Method of a Child Class Overriding That of Its Parent (PHP 4 Syntax) Assign default property value Changing the Value of a Property from Within a Method Define boolean Class properties Adding Properties Adding the $age variable to Cat Class including a complete collection of get and set methods. Controlling Access to Class Members Define a setter and getter Class static properties clone class instance Change class instance after clone clone a class Cloning an aggregated class Copying Objects Parents clone Properly implementing cloning in aggregated classes Constant definition is considered a static member of the class Counting the elements in an array Finding the size of an array Using Class Constants Using Class Constants in PHP5 Instantiate class by calling the constructor Define and use constructor Define class as constructor parameter Define Constructor for Class Adding a Constructor to PriceItem Calling the constructor of the parent class Constructors and Destructors Defining an object constructor Creating the Cat constructor Defining object constructors in PHP 4 Using the PHP 5 style constructor invoking parent constructors Using Unified Constructors and Destructors Using Default Constructors destuctor in action Class destructors Accessing instance-specific data within a destructor Class with destructors Cleaning Up with the __destruct Method (PHP 5 Only) Defining an object destructor Declaring and Using Object Constructors and Destructors extends in action final class Declaring Final Classes and Methods Final Methods final keyword can be used to declare a class uninheritable The final keyword is used to declare that a method or class cannot be overridden by a subclass Call getter to get value for property Define getter and setter Define and call getter method subclass and parent class Three levels of inheritance extends and implement Basic Inheritance Class Inheritance Class Member Binding in PHP Define an Executive class that inherits Employee inheritance example Using the extends keyword to define a subclass Example of Inheritance Overriding parent methods Using inheritance to efficiently represent various vehicle types Using the parent construct is_ainstanceof operator Implement an interface Different class implements one interface A Sample Interface Implementing Multiple Interfaces Defining an interface Defining and Using an Interface Using Type Hinting with Interfaces Method override for Rectangle class Class method override Demo The Method of a Child Class Overriding That of Its Parent Calling an Overridden Method Accessing Attributes of Objects Changing the Value of an Attribute within a Function Convert variables from arrays to objects and back again Comparing Objects with == and === Create a new class and create an instance then use its property and method Object Initialization Object Overloading Object Properties Object Type Information Objects Within Objects Creating a new object and assigning it to a variable class member variable accessAccess private properties with for each loop Changing the member $a to protected or private Using private and public in Classes If the variable was defined as private static, it would not be possible to access it directly Private and inheritance Private properties are accessible only inside the methods of the class that defined them Protect the class from being misused by accessing the members directly Properties get Demo Use poroperty set set property protected member variable Class members' visibility Class using access control Private and protected variables Properties and methods marked as protected are accessible only through the object that owns them Use public properties directly Public fields public method A Class with Public Properties Using Class Visibility Operators get_declared_classesGet class name Get class informationClass informationClass informationUse Reflection class to export class var_dump a class get_class_varsis_subclass_ofget_parent_classget_class_methods is_callablein_array Call class method dynamically method_exists Get Class methodClass method infoClass method infoClass method infoGet class constructor's parameter information class_existsDefine class helper class to check the method existance call_user_func__callUse self to reference static class properties static class properties static class members Using the - and Using Static Methods and Properties to Limit Instances of a Class (PHP 5 Only) Use the static modifier to change a member or method Use this keyword to reference interal properties Use this keyword $this keyword refers to the instance from within the class definition Accessing the $age variable using this- class toString Creating an __autoload() Function Automatically Loading Include Files with ___autoload() __autoload( ) is called whenever you try to create an object of a class that hasn't been defined __autoload() function automatically includes a class file. Using the __autoload() Function __call( ) method is called if PHP fails to find the method Intercepting Method Calls with the __call() Method (PHP 5 Only) Using the __call() Method Using the __clone() Method Using the clone Statement __get( ) specifies what to do if an unknown property is read from within your script Intercepting Property Access with __get() and __set() (PHP 5 Only) Enforcing property access using magic accessor methods __set( ) method is called whenever an undefined property is set in your scripts Controlling serialization using __sleep() and __wakeUp() __sleep() method is called by serialize() before it packs up the object. __toString( ) set a string value for the object that will be used if the object is ever used as a string. Defining a class's stringification Using the __toString() Method __wakeup() is called by unserialize(). Using __sleep() and __wakeup() for Objects

Php Code : Using An Aggregated Class

Using an aggregated class in php

class Address { 
  protected $city;
  protected $country;
  public function setCity($city) { $this->city = $city; }
  public function getCity() { return $this->city; } 
  public function setCountry($country) { $this->country = $country; }
  public function getCountry() { return $this-> country;}

class Person {
 protected $name;
 protected $address;

 public function __construct() { $this->address = new Address; }
 public function setName($name) { $this->name = $name; }
 public function getName() { return $this->name; }
 public function __call($method, $arguments) { 
  if (method_exists($this->address, $method)) { 
   return call_user_func_array( array($this->address, $method), $arguments);