Object and Classes - ranjanui/php GitHub Wiki
- Introduced in version 4, rewritten in php5
- Classes can be considered as a collection of methods, variables and constants. They often reflect a real-world thing, like a Car class or a Fruit class. You declare a class only once, but you can instantiate as many versions of it as can be contained in memory. An instance of a class is usually referred to as an object.
Defining and using class
- => is used with array indexing
- -> is similar to . operator in other language. eg: $this->name
<?php
class User{
}
$user = new User();
?>
class User
{
public $name;
public $age;
public function Describe()
{
return $this->name . " is " . $this->age . " years old";
}
}
$user = new User();
$user->name = "John Doe";
$user->age = 42;
echo $user->Describe();
Constructor and destructor
- special functions which are automatically called when an object is created and destroyed
<?php
class Animal
{
public $name = "No-name animal";
public function __construct($name)
{
$this->name = $name;
}
}
$animal = new Animal("Bob the Dog");
echo $animal->name;
?>
<?php
class Animal
{
public $name = "No-name animal";
public function __construct($name)
{
echo "I'm alive!";
$this->name = $name;
}
public function __destruct()
{
echo "I'm dead now :(";
}
}
$animal = new Animal("Bob");
echo "Name of the animal: " . $animal->name;
?>
Access modifier
- 3 modifier -> private, protected and public
Inheritannce
class Animal{
public $name;
public function Greet(){
return "Hello, I'm some sort of animal and my name is " . $this->name;
}
}
class Dog extends Animal{
public function Greet(){
return "Hello, I'm a dog and my name is " . $this->name;
}
}
$animal = new Animal();
echo $animal->Greet();
$animal = new Dog();
$animal->name = "Bob";
echo $animal->Greet();
Abstract Class
- can't instantiated. Instead, you typically inherit a set of base functionality from them in a new class.
abstract class Animal{
public $name;
public $age;
public function Describe(){
return $this->name . ", " . $this->age . " years old";
}
abstract public function Greet();
}
class Dog extends Animal{
public function Greet(){
return "Woof!";
}
public function Describe(){
return parent::Describe() . ", and I'm a dog!";
}
}
$animal = new Dog();
$animal->name = "Bob";
$animal->age = 7;
echo $animal->Describe();
echo $animal->Greet();
Static classes
- Both variables and methods on a class can be declared as static(shared variables)
- Static method cannot access non-static variables and methods
- Accessing static members require the double-colon operator instead of the -> operator
<?php
class User{
public $name;
public $age;
public static $minimumPasswordLength = 6;
public function Describe(){
return $this->name . " is " . $this->age . " years old";
}
public static function ValidatePassword($password){
if(strlen($password) >= self::$minimumPasswordLength)
return true;
else
return false;
}
}
$password = "test";
if(User::ValidatePassword($password))
echo "Password is valid!";
else
echo "Password is NOT valid!";
?>
Class constant
<?php
class User{
const DefaultUsername = "John Doe";
const MinimumPasswordLength = 6;
}
echo "The default username is " . User::DefaultUsername;
echo "The minimum password length is " . User::MinimumPasswordLength;
?>
final keyword
- final class can not be inherited
- final function can not be overridden
final class Animal{
public $name;
}
class Animal{
final public function Greet(){
return "The final word!";
}
}