×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
/id:(.*?) submit date:(.*?) done date:(.*?) stat:(.*?) err:(.*)/
The result of the above format is:7439722864896249924 sub:001 dlvrd:001
/id:(.*?) (?:sub:\d+ dlvrd:\d+ )?submit date:(.*?) done date:(.*?) stat:(.*?) err:(.*)/
The result:7439722864896249924
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 !
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();
true
but in PHP it evaluates to false
if ("0") {
}
123
will be encrypted and then decrypted back with AES-128-ECB
encryption algorithm.
Encrypts the text using the secret key sec key
function encrypt_text($text, $secret_key)
{
return openssl_encrypt($text,"AES-128-ECB", $secret_key);
}
Decrypts encrypted text (first parameter) using the secret key (second parameter)function decrypt_text($encrypted_string, $secret_key)
{
return openssl_decrypt($encrypted_string,"AES-128-ECB", $secret_key);
}
Calls the above two functions to perform encryption and decryption// Secret key, that is used to encrypt and decrypt the $text
content
$secret = 'sec key';
// the text that we want to encryption and decryption
$text = '123';
$password_encrypted = encrypt_text($text, $secret);
$password_decrypted = decrypt_text($password_encrypted, $secret);
echo 'Encrypted text: ' . $password_encrypted . '<br>';
echo 'Decrypted password: ' . $password_decrypted;
$argv
contains filename and parameters, when the php script
is called through command line (cmd). test_args.php
file content:<?php
echo 'Filename:'.$argv[0]."; ";
echo 'First parameter:'.$argv[1]."; ";
echo 'Second parameter:'.$argv[2]."; ";
If we run the command: php test_args.php first-param second-param
in cmd
the script above will output the following:Filename:test_args.php; First parameter:first-param; Second parameter:second-param;
who()
method of the child class B
from parent class A
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
The code will call who()
method of parent class and print its name A
If we need test()
to call who()
method of child class B
from parent class, then we can use late static binding
technique.
To achieve it, we use static
keyword: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();
Interface IBar {
public function method3();
}
Interface IArray {
public function method2();
}
Interface IFoo extends IBar, IArray {
public function method1();
}
class class1 implements IFoo {
public function method1() {
}
public function method2() {
}
// public function method3() {
// }
}
Class class1
implements IFoo
interface which extends other two interfaces itself.
Because the class has not implemented method3
method (commented), then the following error will be generated:Fatal error: Class class1 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (IBar::method3)
class Bill {
public $dinner = 20;
public $dessert = 5;
public $drink = 3;
public $bill = 0;
public function dinner($count) {
$this->bill += $count * $this->dinner;
return $this;
}
public function dessert($count) {
$this->bill += $count * $this->dessert;
return $this;
}
public function drink($count) {
$this->bill += $count * $this->drink;
return $this;
}
}
$bill = new Bill;
We can call several methods one by one and print the $bill
:$bill->dinner(3);
$bill->dessert(2);
$bill->drink(1);
echo $bill->bill;
Or we can combine the above 4 expressions using method chaining. The result will be the same:echo $bill->dinner(3)->dessert(2)->drink(1)->bill;
new
keyword followed by the class name(new Foo)->nonStaticMethod();
Another method is to use self
which comes after new
keyword(new self)->nonStaticMethod();
Complete example:class Foo {
public function nonStaticMethod()
{
return 'non-static';
}
public static function staticMethod()
{
// return (new Foo)->nonStaticMethod();
return (new self)->nonStaticMethod();
}
}
echo Foo::staticMethod();
The second method new self
is better, because if we want to rename the class later, we will not need to change the class name more than one place