Category Archives: PHP

REST

REST stands for “REpresentational State Transfer”. It is a concept or architecture for managing information over the internet. REST concepts are referred to as resources. A representation of a resource must be stateless. It is usually represented by JSON.

API stands for “Application Programming Interface”. It is a set or rules that allows one piece of software application to talk to another. Those “rules” can include create, read, update and delete operations.

Client-Server: This constraint operates on the concept that the client and the server should be separate from each other and allowed to evolve individually.

Stateless: REST APIs are stateless, meaning that calls can be made independently of one another, and each call contains all of the data necessary to complete itself successfully.

Cache: Because a stateless API can increase request overhead by handling large loads of incoming and outbound calls, a REST API should be designed to encourage the storage of cacheable data.

Uniform Interface: The key to the decoupling client from server is having a uniform interface that allows independent evolution of the application without having the application’s services, or models and actions, tightly coupled to the API layer itself.

Layered System: REST APIs have different layers of their architecture working together to build a hierarchy that helps create a more scalable and modular application.

Code on Demand: Code on Demand allows for code or applets to be transmitted via the API for use within the application.

Unlike SOAP, REST is not constrained to XML, but instead can return XML, JSON, YAML or any other format depending on what the client requests. And unlike RPC, users aren’t required to know procedure names or specific parameters in a specific order.

One of the disadvantages of RESTful APIs is that you can lose the ability to maintain state in REST, such as within sessions. It can also be more difficult for newer developers to use.

is_soap_fault

This function is useful to check if the SOAP call failed, but without using exceptions. To use it, create a SoapClient object with the exceptions option set to zero or FALSE. In this case, the SOAP method will return a special SoapFault object which encapsulates the fault details (faultcode, faultstring, faultactor and faultdetails).

If exceptions is not set then SOAP call will throw an exception on error. is_soap_fault() checks if the given parameter is a SoapFault object.

use_soap_error_handler

This function sets whether or not to use the SOAP error handler in the SOAP server. It will return the previous value. If set to TRUE, details of errors in a SoapServer application will be sent to the client as a SOAP fault message. If FALSE, the standard PHP error handler is used. The default is to send error to the client as SOAP fault message.

XML Character Encoding

Source Encoding

• Conducted at time of parsing
• Cannot be changed during parser lifetime
• Types

  1. UTF-8 (php uses this type for internal document representation; bytes up to 21)
  2. US-ASCII (single byte)
  3. ISO-8859-1 (single byte; default)

Target Encoding

• Conducted at time of php passing data to xml handlers
• Target encoding initially set to same as source encoding
• Can be changed at any time

Characters not capable of source encoding cause an error

Characters not capable of target encoding are demoted (to “?”)

Magic Constants

Definition

• PHP Provides a set of predefined constants defined by the php core (EX: E_ERROR; TRUE)

• Several of these can change, depending upon where they are used.

Therefore, not true constants (EX: __DIR__; __NAMESPACE__).

• __DIR__: returns the current working directory
• __FILE__: returns the current working directory and file name
• __FUNCTION__: returns the current function name
• __CLASS__: returns the current class and namespace if defined
• __LINE__: returns the current line number at the point of use
• __METHOD__: returns the current method name
• __TRAIT__: returns the trait name including namespace if defined.
• __NAMESPACE__: returns the current namespace

Example

ClassName::class: returns the fully qualified class name

<?php
 namespace NS {
 class ClassName {}
 echo ClassName::class;
 }
?>

It outputs NS\ClassNam

Constants

Definition

• Identifier for a value that does not change once defined

Naming

• Start with a letter or underscore, are case sensitive, contain only alphanumeric characters and underscores
• By convention use only uppercase letters

Access

• May be defined and accessed anywhere in a program • must be defined before use; cannot be changed subsequently

Variable

Naming

• Start with a “$”
• Can contain letters, numbers, and underscores
• Must start with letter or underscore and by convention should start with a lower case letter or underscore
• Case-sensitive

Referencing
• Variables can be assigned by value or by reference
• The ampersand (&) creates a reference, or alias, and causes both the original variable and alias to point to the same memory value

Initializing

• Variable typing is set automatically by the php parser and called “type juggling/coercion”
• Initializing variables empty is a good practice if it is possible it already points to memory values and you want to start empty
• The function isset() returns a boolean on a passed variable containing a value other than null string, null, or zero

Type Juggling

Type Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

<?php
 $phpcodez = "1"; // $phpcodez is string (ASCII 49)
 $phpcodez *= 2; // $phpcodez is now an integer (2)
 $phpcodez = $phpcodez * 1.3; // $phpcodez is now a float (2.6)
?>

__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.

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)
  • SOAP Clients in PHP are hiding the complexity of sending
    a request to a remote SOAP Server and processing the
    response