Saturday, August 10, 2013

OOP

You can not perform perfect OOP in PHP. It is rather a knock off version.

In OOP, variables are referred to as attributes, and fuctions are methods


Polymorphism doesn't really work in PHP although there are some hackarounds



class ClassName//It is good practice to capitalize the name
{
   //All of the object that you make will automatically be assigned these variables
   public $var1;
   private $var2;
   protected $var3;
}
_________________________________________________________

Encapsulation allows you to hide the code behind an interface of sorts

public: anyone can get the information and make changes to this variable
private: can only be chages by methods or functions that reside within the class
protected: is also encapsulated


class Animal
{
   public $name;//this doesn't need any function because it is public
   private $age;//this will need a function
   const POUNDID = 12345;
}

   //The Constructor File
   public function __construct($age)//user can set the age
      {
         echo "__constructor created";
         $this->setAge($age);
      }
   //This file will be called every time you create a new object
   
   
public function setAge($sentAge)
   {
      $this->age = $sentAge;
   }

   public function getAge()
   {
      return $this->age;
   }

$this is referring to the object itself; age is a reference to the variable age inside of the object. Anytime you want to refer to variables inside of objects you have to use the $this keyword.

________________________________________________________


Create the object


$dog = new Animal();//Creates a new object from of the animal class

$dog->name = "Grover";//Assign a value to the public attribute "$name"

$dogName = $dog->name;//Retrieves the value from the public attribute 

Set the $age


$dog->setAge(8);//Calls the function
echo "The dogs name is" . $dogname . "<br>";
echo "The dogs age is" . $dog->getAge() . "<br>";


class Animal
{   
   private $name;   
   private $favFood = "Meat";  
   
   public static $numOfAnimals = 0;
   public function __construct($name="No Name")
   {
      echo "__constuct was called";
      $this->setName($name);//call the setName function
      self::$numOfAnimal++;//Access the static variable and increase it's value by one
   }


A static attributes value is shared by all objects of a class. If one object changes the value of this shared variable, it is reflected in all the objects the class




Destroy an object when it is no longer needed


   }
   public function __destruct()
   {
     echo "Destroyed";
   }

   Final
   ______________________

   final public function getName()
   {
      return $this->name;
   }

   final public        function setName($sentName)
   {
      $this->name = $sentName;
   }



   Final says not to let this function to be overridden by any    other methods whenever a new class* extends this animal    class


   Inheritance occurs whenever you base a new class from an    existing class. When you extend a class you are given it's    attributes and methods which you may then modify by    overriding certain attributes and classes, however; you cannot    override a function declared as final

   public function makeNoise()
   {
      echo "Grrrr <br>";
   }

   public function favFood()
   {
      echo "My favorite food is " . $this->faveFood . "<br>";

   public function move()
   {
      echo "Walk around <br>";
   }
}//End Animal class

Extend the Animaclass

class Dog extends Animal
{
   public function __constuct($name="Null")
   {
      //Call the parent construct Method
      parent::__construct($name);
      //Or Animal in place of parent
   }
   public function __destruct()
   {
      echo "__Destroy!";
   }    
   //Override a function by recreating it 
   public function makeNoise()
   {
      echo "Ruff! Ruff! <br>";
   }
}

Create a new object of the dog class

$grover = new Dog("Grover");//This is putting the string Grover into the $name attribute which is stored in the public function __constuct parameter, which would otherwise be null
$grover = new Dog("Paul");
$grover->makeNoise();

$grover->favFood();
$grover->move();

echo Animal::$numOfAnimals . "<br>";












No comments:

Post a Comment