Professional Documents
Culture Documents
class
Key point:
Every class definition begins with the keyword class, followed by a class name, which can be any name
that isn't a reserved word in PHP. Followed by a pair of curly braces, which contains the definition of the
classes members and methods. A pseudo-variable, $this is available when a method is called from within
an object context. $this is a reference to the calling object (usually the object to which the method belongs,
but can be another object, if the method is called statically from the context of a secondary object). This is
illustrated in the following examples:
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
class B
{
function bar()
{
A::foo();
}
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>
<?php
class SimpleClass
{
// member declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
The default value must be a constant expression, not (for example) a variable, a class member or a
function call.
<?php
class SimpleClass
{
// invalid member declarations:
public $var1 = 'hello '.'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
}
?>
Note: There are some nice functions to handle classes and objects. You might want to take a look at the
Class/Object Functions.
Instantiation
To create an instance of a class, a new object must be created and assigned to a variable. An object will
always be assigned when creating a new object unless the object has a constructor defined that throws an
exception on error. Classes should be defined before instantiation (and in some cases this is a
requirement).
<?php
$instance = new SimpleClass();
?>
In the class context, it is possible to create a new object by new self and new parent.
When assigning an already created instance of a class to a new variable, the new variable will access the
same instance as the object that was assigned. This behaviour is the same when passing instances to a
function. A copy of an already created object can be made by cloning it.
<?php
$assigned = $instance;
$reference =& $instance;
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
The above example will output:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
Inheritance:
It is not possible to extend multiple classes, a class can only inherit one base
class.
A class can inherit methods and members of another class by using the extends keyword in the
declaration. It is not possible to extend multiple classes, a class can only inherit one base class.
The inherited methods and members can be overridden, unless the parent class has defined a method as
final, by redeclaring them with the same name defined in the parent class. It is possible to access the
overridden methods or static members by referencing them with parent::
<?php
class ExtendClass extends SimpleClass
{
// Redefine the parent method
function displayVar()
{
echo "Extending class\n";
parent::displayVar();
}
}
Extending class
a default value
Autoloading Objects:
You may define an __autoload function which is automatically called in case
you are trying to use a class which hasn't been defined yet.
The __autoload() method is called only once for each new class that needs to be
loaded. Subsequent instantiation of the Customer class object will not call the
__autoload() method again.
The magic method __autoload() function is a convenience that allows you to use classes without having
to explicitly write code to include them.
The magic method __autoload() is not included in your class definition as this is to be called once in a
script. The best place to put the autoload() file is in your configuration file which is loaded in all your
other scripts.
Many people debate that the magic method __autoload() causes a performance overhead. Well, that is not
the case. There is no performance penalty to pay. In fact, there may be performance improvements if not
all classes are used all the time. This is explained below.
Using the magic method __autoload has the beneficial side effect of requiring strict naming conventions
for files that hold class definitions.
include customer.php;
include orders.php;
$c = new Customer();
In the example displayed above, an instance of class Customer is created. Therefore, we only need the
customers.php file. The file orders.php is not needed. This means that we should only have included the
customer.php file. But, what if during execution on the basis of a condition, an instance of class Orders
would have to be created. Therefore you need to include both the files i.e. customer.php and orders.php
But this causes performance issues. Each time the above script is executed, orders.php is included. To
avoid this performance hit, we would have to do additional programming to ensure that the file orders.php
is loaded only when needed.
This is the reason why magic method __autoload() should be used. Look at the example below:
function __autoload($class) {
require $class . '.php'; //is substituted as require Customer.php (with
capital 'C')
}
$c = new Customer();
In the above program, we dont explicitly include customer.php and orders.php file. When an instance of
the customer class is to be created, the PHP engine checks to see if the file
Customer.php is loaded. It does not raise an warning on finding that Customer.php has not been loaded, it
in turn calls the magic method __autoload(). The __autoload() magic method accepts a parameter which
is the name of the class that needs to be loaded.
Therefore, on the line when an instance of the customer class is created i.e. object $c, magic method
__autoload() is called with the parameter $class containing value Customer. Within the __autoload()
method we call the require method. The require method tries to load $class.php file i.e. Customer.php.
Therefore, as stated earlier, the __autoload() method has its beneficial side effect of requiring strict file
naming convention.
The __autoload() method is called only once for each new class that needs to be loaded. Subsequent
instantiation of the Customer class object will not call the __autoload() method again. Therefore, this
offers performance improvements in your scripts because, unless the class is needed - files are not loaded.
Therefore, the PHP engine does not have to parse and compile an unnecessary file.
Many developers writing object-oriented applications create one PHP source file per-class definition. One
of the biggest annoyances is having to write a long list of needed includes at the beginning of each script
(one for each class).
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 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.
Note: Exceptions thrown in __autoload function cannot be caught in the catch block and results in a fatal
error.
Note: If the class name is used e.g. in call_user_func() then it can contain some dangerous characters such
as ../. It is recommended to not use the user-input in such functions or at least verify the input in
__autoload().
This example attempts to load the classes MyClass1 and MyClass2 from the files MyClass1.php and
MyClass2.php respectively.
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
PHP 5 allows developers to declare constructor methods for classes. 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.
<?php
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
Destructor:
The destructor method will be called as soon as all references to a particular
object are removed or when the object is explicitly destroyed or in any order in
shutdown sequence.
Like constructors, parent destructors will not be called implicitly by the engine .
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The
destructor method will be called as soon as all references to a particular object are removed or when the
object is explicitly destroyed or in any order in shutdown sequence.
<?php
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent
destructor, one would have to explicitly call parent::__destruct() in the destructor body.
Note: Destructors called during the script shutdown have HTTP headers already sent. The working
directory in the script shutdown phase can be different with some SAPIs (e.g. Apache).
Note: Attempting to throw an exception from a destructor (called in the time of script termination) causes
a fatal error.
Visibility:
.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.
Class members must be defined with public, private, or protected.
The visibility of a property or method 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.
Members Visibility
<?php
/**
* Define MyClass
*/
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// We can redeclare the public and protected method, but not private
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
?>
Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility
reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an
E_STRICT warning.
Method Visibility:
<?php
/**
* Define MyClass
*/
class MyClass
{
// Contructors must be public
public function __construct() { }
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// This is public
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // Fatal Error
}
}
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
the double colon, is a token that allows access to static, constant, and
overridden members or methods of a class.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double
colon, is a token that allows access to static, constant, and overridden members or methods of a class.
When referencing these items from outside the class definition, use the name of the class.
As of PHP 5.3.0, it's possible to reference the class using a variable. Keywords like self, parent or static
are not allowed in dynamic class references.
Paamayim Nekudotayim would, at first, seem like a strange choice for naming a double-colon. However,
while writing the Zend Engine 0.5 (which powers PHP 3), that's what the Zend team decided to call it. It
actually does mean double-colon - in Hebrew!
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
echo MyClass::CONST_VALUE;
?>
Two special keywords self and parent are used to access members or methods from inside the class
definition.
<?php
class OtherClass extends MyClass
{
public static $my_static = 'static var';
OtherClass::doubleColon();
?>
When an extending class overrides the parents definition of a method, PHP will not call the parent's
method. It's up to the extended class on whether or not the parent's method is called. This also applies to
Constructors and Destructors, Overloading, and Magic method definitions.
<?php
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
Static Keyword:
Static properties cannot be accessed through the object using the arrow
operator ->.
A member 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 member or method will be
treated as if it was declared as public.
As of PHP 5.3.0, it's possible to reference the class using a variable. Keywords like self, parent or static
are not allowed in dynamic class references.
<?php
class Foo
{
public static $my_static = 'foo';
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
?>
Class Constants:
As of PHP 5.3.0, it's possible to reference the class using a variable. Keywords like self, parent or static
are not allowed in dynamic class references.
<?php
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
$classname = "MyClass";
echo $classname::constant . "\n";
?>
Class Abstraction:
It is not allowed to create an instance of a class that has been defined as
abstract.
Any class that contains at least one abstract method must also be abstract.
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.
PHP 5 introduces abstract classes and methods. Methods defined as abstract simply declare the method's
signature they cannot define the implementation.
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.
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
Object Interfaces:
Object interfaces allow you to create code which specifies which methods a class
must implement, without having to define how these methods are handled.
All methods declared in an interface must be public, this is the nature of an
interface.
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of
the methods having their contents defined.
implements
To implement an interface, the implements operator is used. All methods in the interface must be
implemented within a class; failure to do so will result in a fatal error. Classes may implement more than
one interface if desired by separating each interface with a comma.
Note: A class cannot implement two interfaces that share function names, since it would cause ambiguity.
Examples
<?php
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
return $template;
}
}
?>
Overloading:
__call, __get and __set methods will only be triggered when your object or
inherited object doesn't contain the member or method you're trying to access.
__set() is used when a value is to be assigned to a undefined variable of a class.
__get() is used when value from an undefined variable is to be read.
By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before
using them. This also holds true for using class members. Look at an example below.
<?php
class Customer {
public $name;
}
$c = new Customer();
$c->name = Sunil; // $name is set because its public
?>
Ideally in a strict language this would have been an error. But, with PHP this works perfectly well as you
can assign values to an undefined variable.
Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is
used when value from an undefined variable is to be read and __set() is used when a value is to be
assigned to a undefined variable of a class.
__set() allows you to provide functionality to validate data being stored. See example below:
<?php
class Customer {
public $name;
private $data = array();
$c = new Customer();
$c->name = Sunil; // $name is set because its public
echo $c->email;
?>
In the above example when email@domain.com is assigned to the undefined variable $email, the magic
method __set() is called. To this __set() method the name of the variable is passed into $dt variable of
__set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.
The next step is to store these values into the $data array so that you could retrieve it later.
The __get() method works in the similar fashion. When you echo $c->email, __get() method is called and
the name email is passed in the $dt of the __get() method.
Tip:
It is possible to stop this behavior of PHP to assign values to undefined issues. The solution is that you
raise an exception from within __set() method. Look at the code below:
<?
class Customer {
private $name;
$c = new Customer();
$c->email = email@domain.com; //this will cause an exception to be raised
?>
These methods are automatically called internally when isset() and unset() is called on undeclared data
members.
The magic method __isset() method receives an argument - the value of which
is the name of the variable that the program wants to test if the variable is set
or not.
The magic method __unset() method receives an argument - the value of which
is the name of the variable that the program wants to unset.
class Customer {
private $data = array();
$c = new Customer();
$c->name = Sunil Bhatia;
echo isset($c->name).\n;
echo unset($c->name);
In the example above the script creates a new Customer Object. The program assigns a string value to an
undeclared variable i.e. $c->name. The undeclared variable is handled by the magic method __set
The program ties to check if the undeclared variable i.e., $c->name has been set or not using the PHP
method isset(). Since $c->name is an undeclared variable the PHP5 magic method __isset() is invoked that
takes the name of the undeclared variable i.e. name and checks if the internal array $data[name] is set
or not.
Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5 magic
method __unset() is invoked that takes the name of the undeclared variable i.e. name and unsets the
internal array $data[name].
All overloading methods must not be defined as static. All overloading methods
must be defined as public.
Example 19.20. overloading with __get, __set, __isset and __unset example
<?php
class Setter
{
public $n;
private $x = array("a" => 1, "b" => 2, "c" => 3);
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
echo "Nothing!\n";
}
}
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
echo "OK!\n";
} else {
echo "Not OK!\n";
}
}
return isset($this->x[$nm]);
}
unset($this->x[$nm]);
}
}
var_dump(isset($foo->a)); //true
unset($foo->a);
var_dump(isset($foo->a)); //false
var_dump($foo);
?>
Checking if a is set
bool(true)
Unsetting a
Checking if a is set
bool(false)
bool(true)
object(Setter)#1 (2) {
["n"]=>
int(1)
["x":"Setter":private]=>
array(2) {
["b"]=>
int(2)
["c"]=>
int(3)
}
}
Method overloading
The magic method __call() is automatically called internally when the program
tires to execute a method that has not been defined within the class at the time
of development.
The magic method __call() takes two arguments. The first argument is the name of the undeclared
method invoked by the program and the second is an array that contains a list of parameters passed to the
undeclared array.
class Customer {
$c = new Customer();
$c->setName("Sunil","Bhatia");
Output:
string(7) setName
array(2) {
[0]=>
string(5) Sunil
[1]=>
string(6) Bhatia
}
In the example above, an object of the Customer class is created and an undeclared method viz. $c-
>setName is called. The magic method __call() is internally executed which accepts two parameters. The
first parameter $name contains the name of the method i.e. setName and the second parameter $args
contains the arguments passed to the setName method i.e Sunil & Bhatia.
Using this method, you can provide code to handle calls to undeclared method. To disallow programs to
call an undeclared method, you should raise an exception from within __call() magic method.
class Customer {
$c = new Customer();
$c->setName("Sunil","Bhatia");
Output:
Fatal error: Uncaught exception Exception with message Undeclared method execution not allowed in
D:sunilbwebsiteprogsmagic_call.php:6
Stack trace:
#0 [internal function]: Customer->__call(setName, Array)
#1 D:sunilbwebsiteprogsmagic_call.php(11): Customer->setName(Sunil, Bhatia)
#2 {main}
thrown in D:sunilbwebsiteprogsmagic_call.php on line 6
In the above program, when the script calls an undeclared variable $c->setName(), the magic method
__call() is executed. On executing the magic method __call(), an exception is raised and the execution of
the program stops there (unless we use the try..catch statements)
Magic Methods
The function names __construct, __destruct, __call, __get, __set, __isset, __unset (see Overloading),
__sleep, __wakeup, __toString, __set_state and __clone are magical in PHP classes. You cannot have
functions with these names in any of your classes unless you want the magic functionality associated with
them.
serialize
serialize Converts data into an easily-storable and transportable format.
string serialize(data);
mixed data: Data to be serialized
Returns
serialize() converts a variable or value into a format that can easily be stored as
plain text and then restored to its original value using unserialize() . Both the
variable's type and value are stored.
Any variable type can be serialized, except resource pointers. Also, only an
object's member variables are serialized - the object's methods are discarded
during the process.
<?php
$quote = <<<_END_QUOTE_
"Almost at once the No. 37 Penpoint returned to the Featureless Expanse."<br />
An Inanimate Tragedy, Edward Gorey
_END_QUOTE_;
print <<<_END_
<b>Before serialization:</b>
<blockquote>$quote</blockquote>
<b>After serialization:</b>
<blockquote>$serialized_quote</blockquote>
<b>After unserialization:</b>
<blockquote>$unserialized_quote</blockquote>
_END_;
?>
When serializing objects, PHP will attempt to call the member function
__sleep prior to serialization. This is to allow the object to do any last
minute clean-up, etc. prior to being serialized. Likewise, when the object
is restored using unserialize() the __wakeup member function is called.
__sleep() magic method is called when the object of a class is about to be serialized. This magic method
__sleep() does not accept any parameter and returns an array. The array should contain a list of class
members that should be serialized. This means that if you dont wish to serialize a particular class
member, you should not include it in the array. Look at the example below:
class Customer {
private $name;
private $credit_card_number;
$c = new Customer();
$c->setName("Sunil");
$c->setCC("1234567890123456");
$data = serialize($c)."\n";
echo $data."\n";
Output:
O:8:Customer:1:{s:14: Customer name;s:5:Sunil;}
In the above example, you can see that the serialized string data only contains the name of the Customer
Object. This is because the __sleep() magic method returned an array containing only the name data
member.
__wakeup() magic method is the opposite of the __sleep() method. It is called when the object of a class
is about to be unserialized. This magic method __wakeup() does not accept any parameter nor returns
anything. The __wakeup() method is responsible for setup operations after an object has been unserialized.
Look at the example below:
class Customer {
private $name;
private $credit_card_number;
$c = new Customer();
$c->setName("Sunil");
$c->setCC("1234567890123456");
$data = serialize($c)."\n";
var_dump(unserialize($data));
Output:
object(Customer)#2 (2) {
[name:private]=>
string(5) Sunil
[credit_card_number:private]=>
string(16) 1234567890123456
}
In the above example, you can see that after the $c object has been serialized and the output stored in
$data variable, we use the $data variable and pass it to the unserialize(). Before the object is unserizlied
and object created, the __wakeup() method is called. In the __wakeup() method you should ideally make a
database call to fetch data of the missing member variable.
__toString
<?php
class Customer {
private $firstName, $lastName, $email;
$c = new Customer(Sunil,Bhatia,email@domain.com);
?>
Output:
Customer Object is >> Debug message from Customer Class : First Name = Sunil, Last Name = Bhatia,
Email = email@domain.com
See how in this example $c Customer Object got converted into a string type when used with the dot (.)
concatenation operator. In the background the magic method __toString() is automatically called when
such a conversion happens.
The __toString method allows a class to decide how it will react when it is converted to a string.
<?php
// Declare a simple class
class TestClass
{
public $foo;
Hello
Final Keyword
PHP 5 introduces the final keyword, which prevents child classes from
overriding a method by prefixing the definition with final. If the class
itself is being defined final then it cannot be extended.
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
<?php
final class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
Type Hinting:
PHP 5 introduces Type Hinting. Functions are now able to force parameters to be
objects (by specifying the name of the class in the function prototype) or
arrays (since PHP 5.1).
<?php
// An example class
class MyClass
{
/**
* A test function
*
* First parameter must be an object of type OtherClass
*/
public function test(OtherClass $otherclass) {
echo $otherclass->var;
}
/**
* Another test function
*
* First parameter must be an array
*/
public function test_array(array $input_array) {
print_r($input_array);
}
}
<?php
// An instance of each class
$myclass = new MyClass;
$otherclass = new OtherClass;
<?php
// An example class
class MyClass {
public $var = 'Hello World';
}
/**
* A test function
*
* First parameter must be an object of type MyClass
*/
function MyFunction (MyClass $foo) {
echo $foo->var;
}
// Works
$myclass = new MyClass;
MyFunction($myclass);
?>
Type Hints can only be of the object and array (since PHP 5.1) type.
Traditional type hinting with int and string isn't supported.
Exceptions:
PHP 5 has an exception model similar to that of other programming languages. An exception can be
thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the
catching of potential exceptions. Each try must have at least one corresponding catch block. Multiple
catch blocks can be used to catch different classes of exeptions. Normal execution (when no exception is
thrown within the try block, or when a catch matching the thrown exception's class is not present) will
continue after that last catch block defined in sequence. Exceptions can be thrown (or re-thrown) within a
catch block.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to
find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an
"Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
else return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
?>
The above example will output:
0.2
Caught exception: Division by zero.
Hello World
Extending Exceptions
A User defined Exception class can be defined by extending the built-in Exception class. The members
and properties below, show what is accessible within the child class that derives from the built-in
Exception class.
<?php
class Exception
{
protected $message = 'Unknown exception'; // exception message
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
/* Overrideable */
function __toString(); // formated string for display
}
?>
If a class extends the built-in Exception class and re-defines the constructor, it is highly recomended that it
also call parent::__construct() to ensure all available data has been properly assigned. The __toString()
method can be overriden to provide a custom output when the object is presented as a string.
<?php
/**
* Define a custom exception class
*/
class MyException extends Exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0) {
// some code
/**
* Create a class to test the exception
*/
class TestException
{
public $var;
const THROW_NONE = 0;
const THROW_CUSTOM = 1;
const THROW_DEFAULT = 2;
switch ($avalue) {
case self::THROW_CUSTOM:
// throw custom exception
throw new MyException('1 is an invalid parameter', 5);
break;
case self::THROW_DEFAULT:
// throw default one.
throw new Exception('2 isnt allowed as a parameter', 6);
break;
default:
// No exception, object will be created.
$this->var = $avalue;
break;
}
}
}
// Example 1
try {
$o = new TestException(TestException::THROW_CUSTOM);
} catch (MyException $e) { // Will be caught
echo "Caught my exception\n", $e;
$e->customFunction();
} catch (Exception $e) { // Skipped
echo "Caught Default Exception\n", $e;
}
// Continue execution
var_dump($o);
echo "\n\n";
// Example 2
try {
$o = new TestException(TestException::THROW_DEFAULT);
} catch (MyException $e) { // Doesn't match this type
echo "Caught my exception\n", $e;
$e->customFunction();
} catch (Exception $e) { // Will be caught
echo "Caught Default Exception\n", $e;
}
// Continue execution
var_dump($o);
echo "\n\n";
// Example 3
try {
$o = new TestException(TestException::THROW_CUSTOM);
} catch (Exception $e) { // Will be caught
echo "Default Exception caught\n", $e;
}
// Continue execution
var_dump($o);
echo "\n\n";
// Example 4
try {
$o = new TestException();
} catch (Exception $e) { // Skipped, no exception
echo "Default Exception caught\n", $e;
}
// Continue execution
var_dump($o);
echo "\n\n";
?>
C++ Encapsulation
Introduction
Encapsulation is the process of combining data and functions into a single unit called class. Using the
method of encapsulation, the programmer cannot directly access the data. Data is only accessible through
the functions present inside the class. Data encapsulation led to the important concept of data hiding. Data
hiding is the implementation details of a class that are hidden from the user. The concept of restricted
access led programmers to write specialized functions or methods for performing the operations on hidden
members of the class.
Neither too much access nor too much control must be placed on the operations in order to make the class
user friendly. Hiding the implementation details and providing restrictive access leads to the concept of
abstract data type. Encapsulation leads to the concept of data hiding, but the concept of encapsulation must
not be restricted to information hiding. Encapsulation clearly represents the ability to bundle related data
and functionality within a single, autonomous entity called a class.
For instance:
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc();
.........
......... //Other member functions
private:
int x;
float sq;
..........
......... //Other data members
};
In the above example, the data members integer x, float sq and other data members and member functions
sample(),example(char* se),endfunc() and other member functions are bundled and put inside a single
autonomous entity called class Exforsys. This exemplifies the concept of Encapsulation. This special
feature is available in object-oriented language C++ but not available in procedural language C. There are
advantages of using this encapsulated approach in C++. One advantage is that it reduces human errors.
The data and functions bundled inside the class take total control of maintenance and thus human errors
are reduced. It is clear from the above example that the encapsulated objects act as a black box for other
parts of the program through interaction. Although encapsulated objects provide functionality, the calling
objects will not know the implementation details. This enhances the security of the application.
The concept of encapsulation shows that a non-member function cannot access an object's private or
protected data. This adds security,
Features and Advantages of the concept of Encapsulation:
* Enhanced Security:
Perhaps the most important concept of object orientation is abstraction. The basic idea of abstraction is to
hide the low-level implementation details and to provide a simple interface. It provides two important
benefits. One is the simplicity and clarity of coding and second is the ease of modification.
Encapsulation is used to implement abstraction. In encapsulation we combine the data and methods that
operate on the data and put them in a single unit. An interface is provided to interact with this unit. An
example of abstraction is the class definition in which data and methods are combined together.