Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
Returns false if the email is not in a correct format (if the email is not valid)
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $this->addError('email', 'email must be a valid email');
}
by Valeri Tandilashvili
5 years ago
0
PHP
functions
Object Oriented PHP Tutorial
2
Checks username value against the rule: must be at least 6 characters long and at most 12 characters long, only allowed alphanumeric symbols
if (!preg_match('/^[a-zA-Z0-9]{6,12}$/', $username)) {
    $this->addError('username', 'username must be 6-12 chars & alphanumeric');
}
by Valeri Tandilashvili
5 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
Difference between
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)
by Valeri Tandilashvili
5 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
Triggers error if
$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
by Valeri Tandilashvili
5 years ago
0
PHP
functions
Object Oriented PHP Tutorial
2
Checks if the specified key is in the array
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
by Valeri Tandilashvili
5 years ago
0
PHP
functions
2
1. Interface supports multiple inheritance
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 member
abstract 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 !
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
1
Two classes extends
Animal
abstract class and both implement the same method
prey()
differently
abstract 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!
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
2
class implements several interfaces CODE
One class implements several interfaces and takes responsibility to define all methods from both interfaces. Also defines several private methods additionally
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();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
1
interface example CODE
In this example class
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();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
1
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
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP official doc
1
Results: 1578