Superglobals Variable in PHP
Several predefined variables in PHP are “superglobals”, which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
These superglobal variables are:
- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
- $_ENV
Categories: PHP - Hypertext Preprocessor Tags: data type in PHP, Oops in PHP, PHP data type, PHP5, Variable in PHP
Object Oriented Programmings (OOPs) Concept in PHP5
Classes begin with the keyword ‘class’, followed by the class name which the user may choose. The keyword ‘new’ used to create object from a class.
Inside the scope of a current class context, it is possible to create a objects by using keywords ‘self’ and ‘parent’;
eg: new self; new parent;
A class can inherit methods and members of another class by using the keyword ‘extends’;
eg: class inheritedclass extends parentclassname{ }
Any inherited methods and members of the parent class can be overridden, unless the parent class has defined a method by using the keyword ‘final’, by re declaring those methods or members with the same name defined in the parent class.
Visibility of Class member
The visibility of a member property or method of a class can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
Parent and child class can have a private set of private variables that no-one else has access to, other than the class itself even it was inherited. Which means private members of a class can’t be overridden.
Class constants
keyword used to declare a constant is ‘const’. Constants differ from normal variables in that you don’t use the ‘$’ symbol to declare or use them. The value must be a constant expression, and should not be a variable, a class member, result of a mathematical operation or a function call.
Scope resolution operator (::)
The Scope Resolution Operator is a token that allows access to static, constant, and other members or methods of a class. Two special keywords self and parent are used to access members or methods from inside the class definition. Call it by the name of the class, when referencing these from outside the class.
Constructors and Destructors
Constructors
Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
Destructors
The destructor method will be called automatically when as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
parent destructors will not be called implicitly/ automatically by the engine. we need call manually by calling parent::__destruct() in the destructor body.
Autoloading Classes
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
The function __autoload gets the class name whenever any object gets created. so we can include the class definition for the particular class. So the main usage is to avoid include lot of class file in every page.
Static Keyword
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Class Abstraction
When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
It is not allowed to create an instance from a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature they cannot define the implementation.
when overridding an abstarce method or abstarct member property, the visibility would be less restrictive or same; For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
An abstract class that extends another abstract class doesn’t need to define the abstract methods from the parent class.
Overloading
PHP’s interpretation of “overloading” is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.
Overloading in PHP provides means to dynamically “create” properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms “inaccessible properties” and “inaccessible methods” to refer to this combination of declaration and visibility.
All overloading methods must be defined as public.
Member overloading:
void __set ( string $name , mixed $value ) – is run when writing data to inaccessible members.
mixed __get ( string $name ) – is utilized for reading data from inaccessible members
bool __isset ( string $name ) – is triggered by calling isset() or empty() on inaccessible members.
void __unset ( string $name ) – is invoked when unset() is used on inaccessible members.
Method overloading
mixed __call ( string $name , array $arguments ) – is triggered when invoking inaccessible methods in an object context.
mixed __callStatic ( string $name , array $arguments ) – is triggered when invoking inaccessible methods in a static context.
Object Iteration
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
object’s elements can be viewed as array format by using the print_r() function.
Magic Methods
NOTE ON MAGIC METHODS:
- Magic methods are the members functions that is available to all the instance of class
- Magic methods always starts with “__” (double underscore). Eg. __construct
- All magic methods needs to be declared as public
- To use magic method they should be defined within the class or program scope
Various Magic Methods used in PHP 5 are:
- __construct()
- __destruct()
- __set()
- __get()
- __call()
- __toString()
- __sleep()
- __wakeup()
- __isset()
- __unset()
- __autoload()
- __clone()
Categories: OOPs in PHP, PHP - Hypertext Preprocessor Tags: class in PHP5, Oops in PHP, PHP5
Class constants in PHP
keyword used to declare a constant is ‘const’. Constants differ from normal variables in that you don’t use the ‘$’ symbol to declare or use them. The value must be a constant expression, and should not be a variable, a class member, result of a mathematical operation or a function call.
Example #1:
<?php
class MyClass
{
const constant = ‘constant value’;
function showConstant() {
echo self::constant . “\n”;
}
}
echo MyClass::constant . “\n”;
$classname = “MyClass”;
echo $classname::constant . “\n”; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::constant.“\n”; // As of PHP 5.3.0
?>
Categories: OOPs in PHP, PHP5 Tags: class in PHP5, Oops in PHP, PHP5
visibility of class member in PHP5
The visibility of a member property or method of a class can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
Parent and child class can have a private set of private variables that no-one else has access to, other than the class itself even it was inherited. Which means private members of a class can’t be overridden.
Categories: OOPs in PHP Tags: class in PHP5, Oops in PHP, PHP5
Classes and Objects in PHP 5
Classes begin with the keyword ‘class’, followed by the class name which the user may choose. The keyword ‘new’ used to create object from a class.
Inside the scope of a current class context, it is possible to create a objects by using keywords ‘self’ and ‘parent’;
eg: new self; new parent;
A class can inherit methods and members of another class by using the keyword ‘extends’;
eg: class inheritedclass extends parentclassname{ }
Any inherited methods and members of the parent class can be overridden, unless the parent class has defined a method by using the keyword ‘final’, by re declaring those methods or members with the same name defined in the parent class.
Categories: PHP - Hypertext Preprocessor Tags: Oops in PHP, PHP5

.jpg)