Following a request by one of my blog readers, this is a very basic php class, actually there are two classes which should help demonstrate how easy it is to use object oriented PHP. Again. should you have any questions please use the comments or write to me using the form on your left !
The second class extends the first class, which basically means inherits all public methods of the first class.
//start class
class MyClass
{
//define a property in the class
public $prop1 = "I'm a class property!";
//define your functions
public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}
public function __destruct()
{
echo 'The class "', __CLASS__, '" was destroyed.<br />';
}
public function __toString()
{
echo "Using the toString method: ";
//or
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1=$newval;
}
public function getProperty()
{
return $this->prop1;
}
}
//a second class that inherits from the first
class MyOtherClass extends MyClass
{
public function newMethod()
{
echo "From a new method in " . __CLASS__ . ".<br />";
}
}
// Create a new object Add a comment to this line
//instantiate an object
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeleteRemote Hire Yii Developers in India