Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
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
4 years ago
0
PHP
OOP
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
4 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
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
4 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
With quotes:
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
Without quotes:
$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // &lt;a href='test'&gt;Test&lt;/a&gt;
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_COMPAT
by Valeri Tandilashvili
4 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
Polymorphism is - when multiple classes have different functionality but they all share common interface
interface 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);
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
1
1.
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";
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
1
Content of
index.php
file
spl_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
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
1
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()
method
class 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
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
PHP official doc
1
Let's first create
parent
and
grandparent
clases
class 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();
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
PHP official doc
1
Child class can not override final method of the parent class
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()
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
PHP official doc
1
Results: 1580