Reflection
-------------------------------------------------------------------------------------------------
What is reflection:-
Php 5 supports api that adds new functionality to php.
It provides predefined classes, interfaces, and methods
It is hard to learn , so I am giving some examples on this for reference purpose only,
<?php
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
?>
Basic usage of the reflection API:-
<? php
Reflection::export(new ReflectionClass('Exception'));
?>
Example methods on reflection:-
<?php
class ReflectionFunction implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public mixed invoke(mixed* args)
public mixed invokeArgs(array args)
public bool returnsReference()
public ReflectionParameter[] getParameters()
public int getNumberOfParameters()
public int getNumberOfRequiredParameters()
}
?>
Exception handling:-
Php supports exception handling like other programming languages.
It has three blocks try , throw , catch.
<?php
try { $error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
?>
Migrating from php4 to php5:-
Php5 and zend2 improves the performance and capabilities.
1)new reserved keywords are introduced in php5
Ex:-exception , interface , implements , final , abstract , public , private , protected ,try ,
throw , catch , clone and some reflection api is newly introduced in php5
2)some new functions are introduced in php5.
1)array_combine():-Creates an array by using one array for keys and another for its values
2) array_udiff() - Computes the difference of arrays by using a callback function for data comparison
3)substr_compare():-compare the strings with caseinsensitive.
4)str_split():-Convert a string to an array
5)some date functions are newly introduced. idate() - Format a local time/date as integer
date_sunset() - Time of sunset for a given day and location
date_sunrise() - Time of sunrise for a given day and location
3)new object model is introduced in php5.
Predefined classes of PHP 5
exception
php_user_filter
@@@**** php.inidirectives in function reference ,it have what changes we have to do while programs @@@
@@@Description of core php.ini directives.
@@Type juggling:-
PHP does not require (or support) explicit type definition in variable declaration; a variable's
type is determined by the context in which that variable is used. That is to say, if you assign
a string value to variable $var, $var becomes a string. If you then assign an integer value to
$var, it becomes an integer.