|
1.How PHP Supports Encapsulation? Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from out side interference and miss use .To implement this concept , PHP 5 supports public, private, and protected variables and member functions . 2.How php Implement Polymorphism? PHP supports polymorphism in the sense of allowing instance of subclasses to be used in place of parent instances. Define class in php? A class is a blueprint of an object ,variables and functions (called methods),
PHP 4 Class Style: Create first file as ‘PHP4Class.php’ <?php class w3answersPHP4Class { var $my_class; function my_W3method($pritam) { echo "hey, you are in my_W3method ($pritam)!!\n"; echo "The value of my w3class is: "; echo "{$this->my_class }\n"; } } ?>
“$this is a special variable within a class that represents the instance of the objectitself. It is used to access both methods and properties internally within the object”. To create an instance of the w3answersPHP4Class, the new statement is used:
Create Second file as ‘instance.php’ <?php require_once ("PHP4Class.php "); $w3instance = new w3answersPHP4Class(); $w3anotherinstance = new w3answersPHP4Class(); $w3instance->my_class = 150; $w3anotherinstance->my_class = 250; ?> When executed, the $my_class property of the $w3instance object will be set to 150, and the $w3anotherinstance’s $my_class property will be set to 250. The output will be Hey, you are in my_W3method(Mypritam)!! The value of my w3class is 150 PHP 5 Class style: Create first file as ‘PHP5Class.php’ <?php class w3answersPHP5Class { public $my_class; /*we can also make private $my_class; but it will lead you to error when you tryimg to acess the varaiable like Fatal Error: Cannot access private property w3answersPHP5Class::my_class in .... */ public function my_W3method($pritam) { echo "hey, you are in my_W3method ($pritam)!!\n"; echo "The value of my w3class is: "; echo "{$this->my_class }\n"; } } ?> Create Second file as ‘instance.php’ <?php include_once ("PHP5Class.php "); $w3instance = new w3answersPHP5Class(); $w3anotherinstance = new w3answersPHP5Class(); $w3instance->my_class = 150; $w3anotherinstance->my_class = 250; ?> 3.Explain Constructors and Destructors in php? __construct( ) Called when instantiating an object __destruct( ) Called when deleting an object
Constructors and destructors are functions that are called when a new instance of an object is created (constructors) and/or destroyed (destructors). Their primary purpose is to allow for a means to initialize and clean up after an object during its use.Constructors can accept parameters, which are assigned to specific object fields atcreation time. Constructors can call class methods or other functions.Constructors are intuitively useful for initializing class properties,Class constructors can call on other constructors, including those from the class parent.One classic example is a class to access a database back end, where a constructor could make the connection to the database while the destructor closes it.
PHP 4 Constructors
In PHP 4, only constructors were available and were created by defining a function whose name was the same as the class itself: <?php class w3Class { function w3Class($param) { echo "Created a new instance of w3Class!"; } } $w3instance = new w3Class; ?> In PHP 5, this concept has been changed PHP 5 or (5>=) now uses a unified constructor function named __construct(). PHP 5 or (5>=) also uses a unified __destruct() method for its destructors. PHP 5 Constructors and Destructors <?php class w3Class { function __construct($p) { echo "Created a new instance of w3Class!"; } function __destruct() { echo "Destroyed this instance of w3Class"; } } $w3instance = new w3Class("value"); ?> 4.What is Magic methods in php? There are seven special methods, and they are as follows: __construct( ) Called when instantiating an object __destruct( ) Called when deleting an object __get( ) Called when reading from a nonexistent property __set( ) Called when writing to a nonexistent property __call( ) Called when invoking a nonexistent method __toString( ) Called when printing an object (for eg: converting an object to strings) __clone( ) Called when cloning an object (copying object)
|