__get
and __set
magic methods allows us to get and set any variable without having individual getter and setter methods:class Foo {
protected $properties = array();
public function __get( $key )
{
if(array_key_exists($key, $this->properties)){
return $this->properties[$key];
}
return $this->properties[ $key ];
}
public function __set( $key, $value )
{
$this->properties[ $key ] = $value;
}
}
$foo = new Foo();
$foo->name = 'David';
echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to properties
array__get
and __set
methods are called when there is an attempt either to use or to set a new value to any of the class propertyclass Person{
public $firstName;
public function __get($propertyName){
echo "attempted to read property: $propertyName" . PHP_EOL;
}
public function __set($propertyNane, $propertyValue){
echo "attempted to set new value to property: $propertyNane" . PHP_EOL;
}
}
$p = new Person();
$p->firstName = 'Doe';
echo $p->firstName . PHP_EOL;
$p->lastName = 'John';
echo $p->lastName;
If the property is public
, the methods are not going to be called (if the property does not exist, or exists but is not public, the methods will get called)
If we make $firstName
property private
or protected
, the methods will be called for the $firstName
property
__get
, __set
, __call
and __callStatic
are invoked when the method or property is inaccessiblechildNodes
returns an array of an element's child nodes.
JSfunction setText() {
let a = document.getElementById("demo");
let arr = a.childNodes;
for(let x=0;x<arr.length;x++) {
arr[x].innerHTML = "new text";
}
}
//calling the function with setTimeout to make sure the HTML is loaded
setTimeout(setText, 500);
HTML of the example<div id ="demo">
<p>some text</p>
<p>some other text</p>
</div>
Note: The code above changes the text of both paragraphs to new text
childNodes
returns an array of an element's child nodes.
element.firstChild
returns the first child node of an element.
element.lastChild
returns the last child node of an element.
element.hasChildNodes
returns true if an element has any child nodes, otherwise false.
element.nextSibling
returns the next node at the same tree level.
element.previousSibling
returns the previous node at the same tree level.
element.parentNode
returns the parent node of an element.class Wheather {
public $nonStatic = 0;
public static $tempConditions = ['cold', 'mild', 'warm'];
public static $someProperty = 5;
static function celsiusToFarenheit($celsius) {
return $celsius * 9 / 5 + 32 + $this->nonStatic;
}
}
echo Wheather::celsiusToFarenheit(0);
echo "\n";
In this case we will get the error:
Fatal error: Uncaught Error: Using $this when not in object context
$this
inside static methodsclass Circle {
public static $pi = 3.1415926535;
public $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getArea() {
return pow($this->radius, 2) * self::$pi;
}
public function getPerimeter() {
return $this->radius * 2 * self::$pi;
}
}
$circle1 = new Circle(5);
echo "\nArea: " . $circle1->getArea();
echo "\nPerimeter: " . $circle1->getPerimeter();
<h1>Example heading <span class="badge badge-primary">New</span></h1>
<h2>Example heading <span class="badge badge-secondary">New</span></h2>
<h3>Example heading <span class="badge badge-warning">New</span></h3>
<h4>Example heading <span class="badge badge-success">New</span></h4>
<h5>Example heading <span class="badge badge-secondary">New</span></h5>
<h6>Example heading <span class="badge badge-secondary">New</span></h6>
var myObj = {
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
// Accessing nested object properties
document.write(myObj.cars.car2);
document.write("<br />");
document.write(myObj.cars["car2"]);
document.write("<br />");
document.write(myObj["cars"]["car2"]);
comment
inside the object as a property
{
"employee": {
"name": "George",
"a0ge": "30",
"city": "Tbilisi",
"comments": "The best student ever"
}
}
datetime
objects using createFromFormat
static method of DateTime
built-in class$first_date = DateTime::createFromFormat('Y-m-d', "2020-12-23");
$second_date = DateTime::createFromFormat('Y-m-d', "2020-12-30");
Using diff()
method of DateTime
class we actually calculate differences between the two dates and we get the result in days$difference_between_the_days = $second_date->diff($first_date)->format("%a");
echo $difference_between_the_days;