Class Example
class Triangle{
// a public member can be used from any point of the application:
public $publicMember;
// a protected member can be accessed only from within this class or from its subclasses:
public $protectedMember;
// a protected member can be accessed only from within this class:
private $privateMember;
// the constructor is a method(function) which does not return a parameter
// and is invoked when the object is created:
public function __construct($parameter)
{
echo "Constructor is invoked
";
}
}
Interfaces - an interface defines a set of methods. Each class that implement and interface has to implement the methods defined in the interface.
A class can implement more than one interfaces.
interface Shape
{
public function calculateArea();
}
class Triangle implements Shape{
// because now Triangle implements shape, it has to implement the calculateArea function:
public function calculateArea()
{
return 1000; // for the moment it returns a hardcoded value;
}
}
Invoke parent constructor
class Car
{
public function __construct($color)
{
parent::__construct($color);
print "Creating a car\n";
}
public function accelerate() {
print "Vroom!";
}
}
class Toyota extends Car
{
public function __construct($color) {
parent::__construct($color);
print "Creating a Toyota Car\n";
}
public function accelerate() {
print "Vroom Vroom!";
}
}
Recent Comments