Hi i am Dharmesh Hadiyal , News To Day | News Live | News India | news hindi | news gujrati | update | lates Technology | Bollywood | GTU

Google

Friday, 12 July 2013

PHP 5 | Object Oriented Service |

The release of PHP 5.0, powered by the Zend Engine

2.0, will mark a significant step forward in PHP’s evolu-tion as one of the key Web platforms in the world
today. While keeping its firm commitment to users
 
who prefer using the functional structured syntax of PHP, the new version will provide a giant leap ahead for
those who are interested in its object oriented capabil-ities – especially for companies developing large scale
applications.

1.Basic PHP Constructs for OOP
The general form for defining a new class in PHP is as follows:
class MyClass extends MyParent {
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
//...
}
//...
}
As an example, consider the simple class definition in the listing below, which prints out a box of text in HTML:
class TextBoxSimple {
var $body_text = "my text";
function display() {
print("<table><tr><td>$this->body_text");
print(“</td></tr></table>");
}
}
In general, the way to refer to a property from an object is to follow a variable containing the object with ->and
then the name of the property . So if we had a variable $boxcontaining an object instance of the class TextBox,
we could retrieve its body_textproperty with an expression like:
$text_of_box = $box->body_text;
Notice that the syntax for this access does not put a $before the property name itself, only the $thisvariable.
After we have a class definition, the default way to make an instance of that class is by using the newoperator .
$box = new TextBoxSimple;
$box->display();
The correct way to arrange for data to be appropriately initialized is by writing a constructor function-a special
function called __construct(), which will be called automatically whenever a new instance is created.
class TextBox {
var $bodyText = "my default text";
// Constructor function
function __construct($newText) {
$this->bodyText = $newText;
}
function display() {
print("<table><tr><td>$this->bodyText");
print(“</td></tr></table>");
}
}
// creating an instance

$box = new TextBox("custom text");
$box->display();
PHP class definitions can optionally inherit from a superclass definition by using the extendsclause. The effect
of inheritance is that the subclass has the following characteristics:
• Automatically has all the property declarations of the superclass.
• Automatically has all the same methods as the superclass, which (by default) will work the same way as those
functions do in the superclass.

0 comments:

Post a Comment