Ejemplo n.º 1
0
    public $prop1 = "I'm a class property!";
    public function __construct()
    {
        echo 'The class "', __CLASS__, '" was initiated!<br />';
    }
    public function __destruct()
    {
        echo 'The class "', __CLASS__, '" was destroyed.<br />';
    }
    public function __toString()
    {
        return $this->getProperty();
    }
    public function getProperty()
    {
        return $this->prop1 . "<br />";
    }
}
class MyOtherClass extends MyClass
{
    public function newMethod()
    {
        echo "From a new method in " . __CLASS__ . ".<br />";
    }
}
// Create a new object
$newobj = new MyOtherClass();
// Output the object as a string
echo $newobj->newMethod();
// Use a method from the parent class
echo $newobj->getProperty();
Ejemplo n.º 2
0
        return $this->getProperty();
    }
    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }
    protected function getProperty()
    {
        return $this->prop1 . "<br />";
    }
}
class MyOtherClass extends MyClass
{
    public function __construct()
    {
        parent::__construct();
        echo "A new constructor in " . __CLASS__ . ".<br />";
    }
    public function newMethod()
    {
        echo "From a new method in " . __CLASS__ . ".<br />";
    }
    public function callProtected()
    {
        return $this->getProperty();
    }
}
// Create a new object
$newobj = new MyOtherClass();
// Call the protected method from within a public method
echo $newobj->callProtected();
Ejemplo n.º 3
0
    public function __construct()
    {
        /*
        Preserving Original Method Functionality While Overwriting Methods
        To add new functionality to an inherited method while keeping the original method intact, use the parent keyword with the scope resolution operator (::):
        */
        parent::__construct();
        // Call the parent class's constructor
        echo "A new constructor in " . __CLASS__ . ".<br />";
    }
    public function newMethod()
    {
        echo "From a new method in " . __CLASS__ . ".<br />";
    }
}
$obj3 = new MyOtherClass();
echo $obj3->newMethod();
$obj3->setProperty("Son! this is impressive");
echo $obj3->getProperty();
/**

Reason 1: Ease of Implementation

"While it may be daunting at first, OOP actually provides an easier approach to dealing with data."

While it may be daunting at first, OOP actually provides an easier approach to dealing with data. Because an object can store data internally, variables don't need to be passed from function to function to work properly.

Also, because multiple instances of the same class can exist simultaneously, dealing with large data sets is infinitely easier. For instance, imagine you have two people's information being processed in a file. They need names, occupations, and ages.
The Procedural Approach

Here's the procedural approach to our example:
Ejemplo n.º 4
0
class MyFirstClass
{
    public $property1 = "I am the first property! Woohoo";
    public function __construct($instance)
    {
        echo "I am getting called for object: " . $instance;
    }
    public function set_property1($property1)
    {
        $this->property1 = $property1;
    }
    private function get_property1()
    {
        return $this->property1;
    }
}
class MyOtherClass extends MyFirstClass
{
    public function new_method()
    {
        echo "new method inside extended class.";
        // I can still call the protected method from child classes
        $current_property = $this->get_property1();
        return $current_property;
    }
}
$obj1 = new MyFirstClass('instance of MyFirstClass');
$obj2 = new MyOtherClass('instance of child class');
// the protected access modifier makes the property or method unavailable to any object instance.
echo $obj2->new_method();
// This will actually work!
Ejemplo n.º 5
0
        return $this->prop1 . "<br />";
    }
    public function callProtected()
    {
        return $this->getProperty();
    }
}
class MyOtherClass extends MyClass
{
    public function newMethod()
    {
        echo "From a new method in " . __CLASS__ . ".<br />";
    }
    public function __construct()
    {
        parent::__construct();
        echo "A new constructor in " . __CLASS__ . ".<br />";
    }
    public function __destruct()
    {
        parent::__destruct();
        echo "A new detructor in" . __CLASS__ . ".<br />";
    }
}
// Create a new object
//$newobj = new MyClass;
$newobj = new MyOtherClass();
//echo $newobj;
echo $newobj->callProtected();
echo $newobj->newMethod();
//unset($newobj);