×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
if (!preg_match('/^[a-zA-Z0-9]{6,12}$/', $username)) {
$this->addError('username', 'username must be 6-12 chars & alphanumeric');
}
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();
class User {
public function მარტივიჯამი($num1,$num2) {
return $num1 + $num2;
}
}
class Admin extends User{
}
$admin1 = new Admin('Tom');
echo $admin1->მარტივიჯამი(4,5);
public
- visible from anywhere
private
- visible only for the owner class
protected
- visible only for the owner and it's child classes
class User {
public $username;
public $friends = ['Tom','David'];
function __construct($name) {
$this->username = $name;
print $this->username . "'s object is created'\n";
}
private function მარტივიჯამი($num1,$num2) {
return $num1 + $num2;
}
}
class Admin extends User{
function sumNumbers($num1,$num2) {
return $this->მარტივიჯამი($num1,$num2);
}
}
$admin1 = new Admin('Tom');
echo $admin1->sumNumbers(4,5);
clone
function clones the object and __clone
method is going to call when the object gets cloned class User {
public $username;
public $friends = ['Tom','David'];
function __construct($name) {
$this->username = $name;
print $this->username . "'s object is created'\n";
}
function __clone() {
print $this->username . "'s object is cloned'\n";
}
}
$user1 = new User('Tom');
$user2 = clone $user1;
abstract class Fruit {
private $color;
abstract public function eat();
public function setColor($c) {
$this->color = $c;
}
}
class Apple extends Fruit {
public function eat() {
echo "Omnomnom";
}
}
$obj = new Apple();
$obj->eat();
In this example an interface
would not be able to have method with definition, only method signatures are allowed in interfacesclass Foo {
private $color;
public function bar() {
echo 'before';
$this->color = "blue";
echo 'after';
}
}
// Foo::bar();
// Deprecated
$obj = new Foo;
$obj::bar();
Deprecated: Non-static method Foo::bar() should not be called statically
php.net: In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future
interface IMusician {
public function play();
}
class Guitarist implements IMusician {
public function play() {
echo "playin a guitar";
}
}
In this example Guitarist
child class has to define play()
method because the child class implements interface IMusician
that has the method signaturejson_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";