All posts by Pramod T P

XML

XML stands for eXtensible Markup Language.

XML was designed to store and transport data.

XML was designed to be both human- and machine-readable.

Data format (“UNIVERSAL”) used for structured document exchange.

XML plays an important role in many different IT systems.

XML is often used for distributing data over the Internet.

It is important (for all types of software developers!) to have a good understanding of XML

Example

<?xml version="1.0" encoding="UTF-8"?>
<site>
 <name>PHPCodez</name>
 <url>phpcodez.com</url>
</site>

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 Coercion

In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.

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.

Type Coercion 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
 $phpcodez= 10;   // $phpcodez is an integer
 $bar = (boolean) $phpcodez;   // $phpcodez is a boolean
 ?>

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

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();
 
?>