PHP Topics | Latest Features | Lesser known Facts | Important for Entrance Exams
Reflection in PHP5
$class = new \ReflectionClass(MyClass’);
$method = new \ReflectionMethod('MyClass',’method’);
$args = array();
$method->invoke($method->getDeclaringClass()->newInstanceArgs($args));
Traits in PHP
Is a parent constructor called implicitly in PHP?
Object cloning in PHP
$copy_of_object = clone $object;
When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.
Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
Object iteration in PHP
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
Object Iteration through Implementing Iterator Interface.
Generators in PHP
A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to.
When a generator function is called, it returns an object that can be iterated over. When you iterate over that object (for instance, via a foreach loop), PHP will call the generator function each time it needs a value, then saves the state of the generator when the generator yields a value so that it can be resumed when the next value is required.
Type hinting in PHP
Autoloading files in PHP
Object Serialization in PHP
serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP.unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The methods in an object will not be saved, only the name of the class.
What is $_SERVER in PHP?
What is & operator in PHP?
How to redirect to a URL in PHP?
$url = 'http://www.example.com/';
header('Location: '. $url);
How to use variables defined in outer class in closures?
$var = 'test';
self::func('key',function() use ($var){
});
Calling the same function with variable number of arguments:
Use get_func_args() in the function to get array of arguments.
function demo() {
$args = get_func_args();
for($i=0;$i<get_num_args();$i++) {
//do some processing with each argument
}
}
How to change value of ini variables in PHP?
ini_set(‘variable_name’,’variable_value’);
Output Buffering in PHP
This function will redirect the output to a buffer. We can flush out the buffer ob_end_flush().
list() in PHP
<?php
$result = $pdo->query("SELECT id, name, salary FROM employees");
while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) {
echo " \n" .
" $name \n" .
" $salary \n" .
" \n";
}
?>
Anonymous Classes in PHP7:
<?php
// Pre PHP 7 code
class Logger
{
public function log($msg)
{
echo $msg;
}
}
$util->setLogger(new Logger());
// PHP 7+ code
$util->setLogger(new class {
public function log($msg)
{
echo $msg;
}
});
Late Static Bindings
Limitations of self::
Executing a function name as a string:
$funcName = ‘testFunction’;
$args = array();
call_user_func_array($funcName,$args);
Deleting a file:
unlink($filePath);
File Pattern Matching:
glob()
:
Returns an array of all files in the directory pointed by $fileDir;
$filesList = glob($fileDir . ‘/*’ );
Browser Information:
get_browser(null,true);
will return an array containing all the useful information for an User Agent header
Comments
Post a Comment