Thursday, 10 December 2015

65_OOPS_IN_PHP_6

Preserving the Functionality of the Parent Class


If we are interested to use parent class method into child class with the overriding functionality. So for that to call an overridden method, you write parent:: before the method name:,


parent::someMethod();





Blocking Inheritance and Overrides with Final Classes and Methods


If we are interested to block the overriding the method or class in to the child class so at that time do one thing only specify the “ final” keyword in front of any class or any method so it will behave and if you might want to lock down one or more methods inside a class so that they can ’ t be


overridden in a child class. By doing this, you know that your class — or methods within your class — will always behave in exactly the same way.





When you specify the final key word it will generate the error …





Working with Interfaces


Interface works same as abstract, they declare a consistent set of methods that classes must implement. However, whereas an abstract class has a parent - child relationship with the class that extends it, this relationship doesn ’ t exist with interfaces. Instead, a class implements an interface. (At the same time, the class can also extend a parent class.)


Let’s just elaborate the concept of interface, a television is a very different kind of object to a tennis ball, and each type of object will have very different properties and behaviors. Yet an online retailer might well sell both televisions and tennis balls. By creating a Sellable interface, and making both Television and TennisBall classes implement that interface, you can ensure that both classes contain methods such as sellItem() , deliverItem() , and getStockLevel(),allowing Television and TennisBall objects to be sold in the online store.





Constructors and Destructors


When creating a new object, often it’s useful to set up certain aspects of the object at the same time. For example, you might want to set some properties to initial values, fetch some information from a database to populate the object, or register the object in some way.


Similarly, when it ’ s time for an object to disappear, it can be useful to tidy up aspects of the object, such as closing any related open files and database connections, or unsetting other related objects.


Like most OOP languages, PHP provides you with two special methods to help with these tasks. An object’s constructor method is called just after the object is created, and its destructor method is called just before the object is freed from memory.


In the following sections you learn how to create and use constructors and destructors.


Constructor


Normally, when you create a new object based on a class, all that happens is that the object is brought into existence. (Usually you then assign the object to a variable or pass it to a function.) By creating a constructor method in your class, however, you can cause other actions to be triggered when the object is created.


To create a constructor, simply add a method with the special name __construct() to your class. (That’s two underscores, followed by the word “ construct, ” followed by parentheses.) PHP looks for this special method name when the object is created; if it finds it, it calls the method.


You can also pass arguments to constructors, just like normal methods. This is great for setting certain properties to initial values at the time the object is created. The following example shows this principle in action:





Person class have the 3 private variable, and with the use of the __constructor, in the above program passed the 3 value which is stored in to the class variable which have the private property.


Destructor


Destructors are useful for tidying up an object before it ’ s removed from memory. For example, if an object has a few files open, or contains data that should be written to a database, it ’ s a good idea to close the files or write the data before the object disappears.


You create destructor methods in the same way as constructors, except that you use __destruct() rather than __construct() :


function __destruct()
{
// (Clean up here)
}
Note that, unlike a constructor, a destructor can ’ t accept arguments.
class Person
{
public function save()
{
echo “Saving this object to the database... < br / > ”;
}
public function __destruct()
{
$this- > save();
}
}
$p = new Person();
unset( $p );
$p2 = new Person();
die ( “Something’s gone horribly wrong! < br / >”);
This code displays the following output:
Saving this object to the database...
Something ’ s gone horribly wrong!
Saving this object to the database...
This Person class contains a destructor that calls the object’s save () method to save the object’s contents to a database before the object is destroyed. (In this example, nothing is actually saved; instead the message “Saving this object to the database...” is displayed.)

No comments:

Post a Comment