if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->addError('email', 'email must be a valid email');
}
if (!preg_match('/^[a-zA-Z0-9]{6,12}$/', $username)) {
$this->addError('username', 'username must be 6-12 chars & alphanumeric');
}
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)$divisor
equals to 0$divisor = false;
if ($divisor == 0) {
trigger_error("Cannot divide by zero", E_USER_WARNING);
}
Possible second parameter values:
E_USER_NOTICE
E_USER_WARNING
E_USER_ERROR
The second parameter defaults to E_USER_NOTICE$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
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 !
Animal
abstract class and both implement the same method prey()
differentlyabstract class Animal {
// child classes must implement this
abstract function prey();
public function run() {
echo "I am running!\n";
}
}
class Dog extends Animal {
public function prey() {
echo "I killed the cat !\n";
}
}
class Cat extends Animal {
public function prey() {
echo "I killed the rat !\n";
}
}
$dog = new Dog();
$cat = new Cat();
$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !
$dog->run(); // I am running!
$cat->run(); // I am running!
interface int1 {
// Force Extending class to define these methods
function getArea();
}
interface int2 {
function getPerimeter();
}
class Circle implements int1, int2 {
static $PI = 3.1415926536;
public $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getPI() {
return self::$PI;
}
public function getArea() {
return pow($this->radius, 2) * self::$PI;
}
public function getDiameter() {
return $this->radius * 2;
}
public function getPerimeter() {
return $this->radius * 2 * self::$PI;
}
}
$circle1 = new Circle(5);
echo "\nPI: " . $circle1->getPI();
echo "\nArea: " . $circle1->getArea();
echo "\nDiameter: " . $circle1->getDiameter();
echo "\nPerimeter: " . $circle1->getPerimeter();
Circle
implements CircleAbstract
interface.
Therefore it must define getArea()
and getPerimeter()
methods interface CircleAbstract {
// Force Extending class to define these methods
function getArea();
function getPerimeter();
}
class Circle implements CircleAbstract {
static $PI = 3.1415926536;
public $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getPI() {
return self::$PI;
}
public function getArea() {
return pow($this->radius, 2) * self::$PI;
}
public function getDiameter() {
return $this->radius * 2;
}
public function getPerimeter() {
return $this->radius * 2 * self::$PI;
}
}
$circle1 = new Circle(5);
echo "\nPI: " . $circle1->getPI();
echo "\nArea: " . $circle1->getArea();
echo "\nDiameter: " . $circle1->getDiameter();
echo "\nPerimeter: " . $circle1->getPerimeter();
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
About abstract class:
Classes defined as abstract cannot be instantiated
Any class that contains at least one abstract method must also be abstract
Methods defined as abstract simply declare the method's signature - they cannot define the implementation