示例#1
0
    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();
        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:

 **/
function changeJob($person, $newjob)