Magic Methods-Php





Magic Methods and Constants

Magicmethods are specially named methods that
can be defi nedin any class and are executed via
 builtin PHP functionality.
Magic methods always begin with a double
underscore. In fact, the magic methods  __destruct() and __construct() have already been used several times.

It is often useful to determine where in the code output originates.

This is the purpose of all of the magic  constants and is particularlyuseful when writing custom logging functions.

 The seven magic constants are as follows:
__CLASS__ equates to the class in which the constant is referenced.

 As noted earlier, this variable is always equal to the class in which it is defi ned, which is not always the class  that was instantiated.

For example, __CLASS__ as defi ned inside Node always returns Node even  if the method is part of an object that  was instantiated as a descendant class.
 In addition to debugging, the class constant is also useful for static callback functions.
__FILE__ is always equal to the file name where the constant is referenced.
__LINE__ is used in conjunction with __FILE__ in order  to output a location in code.
Related Posts:
  • What is triggers? A trigger is a database object which is associated with particular database table. Triggers gets called automatically when particular event(INSERT, UPDATE, DELETE) occurs on table. … Read More
  • PHP Redirect - Redirect Script? PHP Redirect Function header('Location: destination.php'); exit();   ou need the Location: part so the browser knows what header it's  receiving. Also, don't forget to  do an exit() or die() right after… Read More
  • Checking Variable Values and Types FUNCTION is_numeric() True if number or numeric stringctype_digit() True if all digits are numeric charactersis_bool() True if variable is a Booleanis_null() True if variable is NULLis_float() True if variable type is a fl o… Read More
  • Setting Default Values for Function Parameters Assign the default value to the parameters inside the function prototype: function wrap_html_tag($string, $tag = 'b') { return "<$tag>$string</$tag>"; }     The example in the Solution sets the… Read More
  • Reading DOC file in php   read PDF and DOC files using PHP Reading PDF Files   $content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');     Reading DOC Files  $content = shell_exec('/usr/local/bin/antiword '.$fi… Read More
  • Extracting Substrings You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.  Extracting a substring with substr( ) &l… Read More
  • You want to extract part of a string, starting at a particular place in the string Use substr( ) to select your substrings: $substring = substr($string,$start,$length); $username = substr($_REQUEST['username'],0,8); … Read More
  • Setting Environment Variables Setting environment variables in your server configuration on a host-by-host basis allows you to configure virtual hosts differently. <?php putenv('ORACLE_SID=ORACLE'); // configure oci extension ?>   Adjusting… Read More
  • PHP Classes Classes do not use scope keywords, but you can prevent people from instantiating the class by makingthe __construct() method and the __clone() methods private or protected. The __construct()method is used to create the objec… Read More
  • Turning an Array into a String convert it into a  formatted string. Use join( ): // make a comma delimited list $string = join(',', $array); Or loop yourself: $string = ''; foreach ($array as $key => $value) { $string .= ",$value"; } $str… Read More
  • convert time in php <?php $thishour = time() + (4*60*60); $newTime = date("d m Y H:i:s",$thishour); echo $newTime; ?>); … Read More
  • Php get_meta_tags-Extracts all meta tag content attributes get_meta_tags — Extracts all meta tag content attributes from a file and returns an array <?php// Assuming the above tags are at www.example.com$tags = get_meta_tags('htt… Read More
  • Finding the Position of a Value in an Array Use array_search( ) . It returns the key of the found value. If the value is not in the array, it returns false:   $position = array_search($value, $array); if ($position !== false) { // the element in position&n… Read More
  • magic_quotes_gpc, magic_quotes_runtime Magic quotes is the name of a PHP feature that automatically quotes inputdata, by using the addslashes() function. Historically, this was used so thatform data could be used directly in SQL queries without any security or qu… Read More
  • Control Structures if else elseif/else if Alternative syntax for control structures while do-while for foreach break continue switch declare return require include require_ once include_ once goto … Read More