class Class_A { }
class Class_B { }
abstract class MyAbstractClass extends Class_A, Class_B { }
Parse error: syntax error, unexpected ',', expecting '{' in...
.
2. Interface contains only incomplete member (contains only signature of a method)
interface Animal {
function run() {
echo 'I am running!';
}
}
Fatal error: Interface function Animal::run() cannot contain body in ...
.
3. Interface does not contain data member
interface MyInterface {
public $foo = null;
}
Fatal error: Interfaces may not include member variables in ...
.
4. Interface can not have access modifiers (other than public
), by default everything is assumed as public
interface iTemplate
{
private function setVariable($name, $var);
public function getHtml($template);
}
Fatal error: Access type for interface method iTemplate::setVariable() must be omitted in ...
.
Notes (mistakes on the page):
1. Interface does contain CONSTRUCTOR (but can force child classes to implement constructor) as well as abstract class can contain one
interface MyInterface {
function __construct();
}
class mathh implements MyInterface {
// function __construct(){ }
}
2. We can have static methods inside interface as well as inside abstract classes
interface MyInterface {
static function getPI();
}
class mathh implements MyInterface {
static function getPI(){
return 3.1415926535;
}
}
3. Abstract class can contain abstract static memberabstract class Animal {
// child classes must implement this
abstract static function prey();
static public function run() {
echo "I am running!\n";
}
}
class Dog extends Animal {
static public function prey() {
echo "I killed the cat !\n";
}
}
$dog = new Dog();
$dog->prey(); // I killed the cat !
empty
and isset
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
The following values are considered to be false:
""
(an empty string)
0
(0 as an integer)
0.0
(0 as a float)
"0"
(0 as a string)
NULL
FALSE
array()
(an empty array)if (!preg_match('/^[a-zA-Z0-9]{6,12}$/', $username)) {
$this->addError('username', 'username must be 6-12 chars & alphanumeric');
}
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
Without quotes:$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // <a href='test'>Test</a>
The second parameter possible values:
ENT_COMPAT
- Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES
- Will convert both double and single quotes.
ENT_NOQUOTES
- Will leave both double and single quotes unconverted.
The default is ENT_COMPATinterface Element {
public function characteristics();
}
class Water implements Element {
public function characteristics() {
return [
'Water characteristic 1',
'Water characteristic 2',
'Water characteristic 3',
];
}
}
class Fire implements Element {
public function characteristics() {
return [
'Fire characteristic 1',
'Fire characteristic 2',
'Fire characteristic 3',
];
}
}
class Air implements Element {
public function characteristics() {
return [
'Air characteristic 1',
'Air characteristic 2',
'Air characteristic 3',
];
}
}
class Earth implements Element {
public function characteristics() {
return [
'Earth characteristic 1',
'Earth characteristic 2',
'Earth characteristic 3',
];
}
}
function describe(Element $element) {
echo get_class($element) . "\n";
$characteristics = $element->characteristics();
if (is_array($characteristics)) {
foreach ($characteristics as $characteristic) {
echo $characteristic . "\n";
}
echo "\n\n";
}
}
$element = new Water;
describe($element);
$element = new Fire;
describe($element);
$element = new Air;
describe($element);
$element = new Earth;
describe($element);
json_decode
returns an instance of stdClass
class$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);
//Example with StdClass
echo get_class($stdInstance) . ':' . PHP_EOL;
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL . PHP_EOL;
//Example with associative array
echo 'associative array:' . PHP_EOL;
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
2. Casting an array to an object returns an instance of stdClass
class$array = array(
'Property1'=>'hello',
'Property2'=>'world',
'Property3'=>'again',
);
$obj = (object) $array;
echo get_class($obj) . ':' . PHP_EOL;
echo $obj->Property3;
User details info represented using an array and an object$array_user = array();
$array_user["name"] = "smith john";
$array_user["username"] = "smith";
$array_user["id"] = "1002";
$array_user["email"] = "smith@nomail.com";
$obj_user = new stdClass;
$obj_user->name = "smith john";
$obj_user->username = "smith";
$obj_user->id = "1002";
$obj_user->email = "smith@nomail.com";
index.php
filespl_autoload_register(function($class) {
// echo 'register class:'.$class."<br>";
require_once("classes/{$class}.php");
});
// echo 'hey there on line 7'."<br>";
$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;
echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();
Content of classes/Talkative.php
file interface Talkative {
public function talk();
}
Content of classes/Cat.php
file class Cat implements Talkative {
public function talk() {
return 'Meow' . '<br>';
}
}
Content of classes/Dog.php
file class Dog implements Talkative {
public function talk() {
return 'Woof' . '<br>';
}
}
Content of classes/Tortoise.php
file class Tortoise implements Talkative {
public function talk() {
return 'Yak yak yak yak ...' . '<br>';
}
}
Filenames and class names must be THE SAME
Late Static Binding
gives us ability to call child class method from parent class.
In this example who()
method of child class will be called from parent class test()
methodclass A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
Another example of late static binding: parent class uses child class property - tableName
class Model
{
protected static $tableName = 'Model';
public static function getTableName()
{
return static::$tableName;
}
}
class User extends Model
{
protected static $tableName = 'User';
}
echo User::getTableName(); // User
parent
and grandparent
clasesclass A {
public static function who() {
echo __CLASS__;
}
}
class B extends A {
public static function who() {
parent::who();
}
}
One way to call grandparent's who
method is to use the grandparent class name A
class C extends B {
public static function who() {
A::who();
}
}
C::who();
Another way is to call who
method of parent class which also calls its parent class (which is grandparent for C
class)class C extends B {
public static function who() {
parent::who();
}
}
C::who();
class A {
final public static function who() {
echo __CLASS__;
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::who();
Fatal error: Cannot override final method A::who()