All posts by Pramod T P

__autoload

PHP introduced the __autoload() function in version 5 which is called whenever the code tries to use a class that has not yet been defined. You simply put some code into __autoload() to include the appropriate class file and don’t have to bother about manually including those files.

This global function is called whenever you try to create an object of a class that hasn’t been defined. It takes just one parameter, which is the name of the class you have not defined. If you define an object as being from a class that PHP does not recognise, PHP will run this function, then try to re-create the object – you have a second chance to have the right class.

Share

SOAP

It is important for web applications to be able to communicate over the Internet.

The best way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.

SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.

  • SOAP stands for Simple Object Access Protocol
  • SOAP is an application communication protocol
  • SOAP is a format for sending and receiving messages
  • SOAP is platform independent
  • SOAP is based on XML
  • SOAP is a W3C recommendation
  • SOAP is also a request-/response-based protocol.
  • SOAP can be transported using SMTP, HTTP and other protocols.
  • SOAP traffic via HTTP can be encrypted and compressed just like other HTTP requests.
  • Requires the LIBXML extension (Enabled by default in PHP)
Share

Class Constants

It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don’t use the $ symbol to declare or use them. The default visibility of class constants is public.

The value must be a constant expression, not (for example) a variable, a property, or a function call.

It’s also possible for interfaces to have constants. Look at the interface documentation for examples.

As of PHP 5.3.0, it’s possible to reference the class using a variable. The variable’s value can not be a keyword (e.g. self, parent and static).

Note that class constants are allocated once per class, and not for each class instance.

  • Class constants are public
  • Class constants are being inherited
  • Class constants can be initialized by const
<?php
 class MyClass {
 const CONSTANT = 'constant value';

function showConstant() {
 echo self::CONSTANT . "\n";
 }
 }
 
 echo MyClass::CONSTANT . "\n";
 
 $class = new MyClass();
 $class->showConstant();
 
?>
Share

Unbuffered Queries

Unbuffered MySQL queries execute the query and then return a resource while the data is still waiting on the MySQL server for being fetched. This uses less memory on the PHP-side, but can increase the load on the server. Unless the full result set was fetched from the server no further queries can be sent over the same connection. Unbuffered queries can also be referred to as “use result”.

Share

ob_start

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

<?php
 function callback($buffer) {
 return (str_replace("PHP", "PHP Codez", $buffer));
 }
 ob_start("callback");
?>
 <p>Welcome to PHP</p>
<?php
 ob_end_flush();
?>

The output will be Welcome to PHP Codez

Share

quote_meta

The quotemeta() function adds backslashes in front of some predefined characters in a string.

This function can be used to escape characters with special meanings, such as ( ), [ ], and * in SQL.

This function is binary-safe.

The predefined characters are:

  • period (.)
  • backslash (\)
  • plus sign (+)
  • asterisk (*)
  • question mark (?)
  • brackets ([])
  • caret (^)c
  • dollar sign ($)
  • parenthesis (())

Example

<?php
 $str = "PHP Codez. (Welcome)";
 echo quotemeta($str);
?>

It outputs PHP Codez\. \(Welcome\)

Share

header_remove

It is used to remove previously set headers

<?php
 header("X-Foo: Bar");
 header("X-Bar: Baz");

echo "<pre>";print_r(headers_list());

header_remove("X-Foo"); 
 echo "<pre>";print_r(headers_list());
?>

Output

Array
(
    [0] => X-Powered-By: PHP/7.1.14
    [1] => X-Foo: Bar
    [2] => X-Bar: Baz
)Array
(
    [0] => X-Powered-By: PHP/7.1.14
    [1] => X-Bar: Baz
)
Share