CSC/ECE 517 Fall 2012/ch1 1w34 vd

From Expertiza_Wiki
Revision as of 21:29, 14 September 2012 by Drai (talk | contribs) (Created page with "Re- implantation of methods in PHP 5 The object model in PHP5 was rewritten to include OO features such as final, abstract and visibility for classes and methods and improve the ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Re- implantation of methods in PHP 5 The object model in PHP5 was rewritten to include OO features such as final, abstract and visibility for classes and methods and improve the overall performance of the language. Re-implantation of methods in Php 5 can be achieved in the following manner:

Object Inheritance This is an important OO feature that PHP uses in its object model. When a subclass extends from a parent class, it inherits all the public and protected methods from the parent class. The issues that affect how the inherited methods behave are:

-Method Visibility All methods that are defined as ‘public’ and ‘protected’ in the parent class are ‘visible’ in the subclass. Public and protected are access modifiers that define the visibility of methods between different classes. Methods defined as ‘private’ access are not visible in the subclass. Methods declared without any access keyword are ‘public’. Example: <?php
 class GameClass
{
    // Declare a public constructor
    public function __construct() { }

    // Declare a public method
    public function GamePublic() { }

    // Declare a protected method
    protected function GameProtected() { }

    // Declare a private method
    private function GamePrivate() { }

    // This is public
    function Play()
    {
        $this->GamePublic();
        $this->GameProtected();
        $this->GamePrivate();
    }
}

$mygame = new GameClass;
$mygame->GamePublic(); // Works
$mygame->GameProtected(); // Fatal Error
$mygame->GamePrivate(); // Fatal Error
$mygame->Play(); // Public, Protected and Private work


class BoardGame extends GameClass
{
    // This is public
    function PlayDice()
    {
        $this->GamePublic();
        $this->GameProtected();
        $this->GamePrivate(); // Fatal Error
    }
}

$myboard = new BoardGame;
$myboard->GamePublic(); // Works
$myboard->PlayDice(); // Public and Protected work, not Private
 -Overriding If a subclass defines a new method with the same name and signature as that of the method from the parent class then an object of the subclass calling that method will execute the implementation of the method defined in the subclass. Example: change this example for overriding <?php
class game
{
    public function game-name($string)
    {
        echo 'Game: ' . $string . PHP_EOL;
    }
    
    public function play()
    {
        echo 'I like this game.' . PHP_EOL;
    }
}

class chess-game extends game
{
    public function game-name($string)
    {
        echo 'Board Game: ' . $string . PHP_EOL;
    }
}

$gm = new game ();
$cg = new chess-game();
$game->game-name('cricket'); // Output: 'Game: cricket'
$gm->play();       // Output: 'I like this game' 
$cg->game-name('chess'); // Output: 'Board Game: chess'
$cg->play();       // Output: 'I like this game'
?>

-Scope Resolution When a subclass extends a class, then the overridden and overloaded methods of the parent class can be called, using the scope resolution operator (::) inside the subclass. <?php class ParentClass {

   protected function parentFunc() {
       echo "ParentClass::parentFunc()\n";
   }

}

class ChildClass extends ParentClass {

   // Override parent's definition
   public function parentFunc()
   {
       // Call the parent function
       parent::parentFunc();
       echo "ChildClass::parentFunc()\n";
   }

}

$class = new ChildClass(); $class->parentFunc(); ?> Output ParentClass::parentFunc() ChildClass::parentFunc()

Class Abstraction Abstract methods in PHP 5 are methods inside abstract class that only provide signature for the method without any actual implementation to it. An abstract method has an abstract keyword at the beginning of the method declaration. A class containing even a single abstract method should be declared as abstract by adding the abstract keyword to its declaration. Any class inheriting from an abstract class should provide implementation to all of the abstract methods or declare it abstract. No class can inherit from more than one abstract class. The inheriting class, should have the same signature for the inherited abstract methods and the same or less restrictive visibility as defined for the methods in the abstract class. <?php
abstract class ParentAbstractClass
{
    // Method without definition
    abstract protected function getName();
    abstract protected function setName($name);

    // Method with definition
    public function printName() {
        print $this->getName() . "\n";
    }
}

class ConcreteChild extends ParentAbstractClass
{

//Method from abstract class implemeted
    protected function getName() {
        return "ConcreteChild";
    }


//Method from abstract class implemeted
    public function setName($name) {
        return "ConcreteChild : {$name}";
    }
}

class ConcreteChild2 extends ParentAbstractClass
{ //Method from abstract class implemeted
    public function getname() {
        return "ConcreteChild2";
    }
 //Method from abstract class implemeted
    public function setName($name) {
        return "ConcreteChild2 : {$name}";
    }
}

$class1 = new ConcreteChild;
$class1->printName();
echo $class1->setName('FOO') ."\n";

$class2 = new ConcreteChild2;
$class2->printName();
echo $class2->setName('FOO') ."\n";
?> The above example will output: ConcreteChild ConcreteChild : FOO ConcreteChild2 ConcreteChild2 : FOO

Object Interfaces Interface in php is a mechanism that specifies methods that a class requires to implement, without having to provide any implementation to these methods inside the interface. An interface is declared like any other class, just that it is prefixed by the interface keyword and none of its methods have any content to them. A class providing implementation to the methods of an interface has the implements keyword followed by the class name for that class. Similarly, an interface can be inherited by another interface so that, a class implementing the interface that is lowest in the inheritance hierarchy gets to implement the methods from all the interfaces. Methods inside the interface being implemented by a class should have the same signature as that of the interface. Note: An interface cannot be instantiated. <?php
interface food
{ //Interface method with no implementation
    public function eat();
}

interface fruit
{ //Interface method with no implementation
    public function fruitName();
}

interface Apple extends food, fruit { //Interface method with no implementation   public function juice();
}

class GreenApple implements Apple
{
//Implementing methods from all the interfaces public function eat()
    { echo “Food: eat()”;     }

    public function fruitName()
    { echo “Fruit: fruitName()”; 
    }

    public function juice()
    { echo “Apple: juice()”; 
    }
}
?> The class GreenApple will implement all the methods that it has inherited from all the interfaces in inheritance hierarchy.