If the name of a class name and function is similar in that case, then the function is known as the constructor.
Manufacturer is a special type of method because its name is similar to class name.
Example-
Manufacturer is a special type of method because its name is similar to class name.
Example-
<?php
class A{
function testA()
{
echo "This is test method of class A";
}
function A(){
echo "This is user defined constructor of class A"."<br/>";
}
}
$obj= new A();
$obj->testA();
?>
Note: Here we have tested the Test () method but we have not called it
Predefine Constructor
Example-
<?php
class A {
function A()
{
echo "user defined constructor";
}
function __construct()
{
echo "This is predefined constructor";
}
}
$obj= new A();
?>
Parametrized constructor
Example-
<?php
class employee{
Public $name;
Public $profile;
function __construct($n,$p)
{
$this->name=$n;
$this->profile=$p;
echo "Welcome ";
}
function show()
{
echo $this->name."... ";
echo "Your profile is ".$this->profile."<br/>";
}
}
$obj= new employee("shashi","developer");
$obj->show();
$obj1= new employee("pankaj","Tester");
$obj1->show();
?>
Destructor in PHP
Note : _ _destruct() is used to define destructor.
Example-
<?php
class demo{
function __construct()
{
echo "object is initializing their propertie"."<br/>";
}
function work()
{
echo "Now works is going "."<br/>";
}
function __destruct()
{
echo "after completion the work, object destroyed automatically";
}
}
$obj= new demo();
$obj->work();
//to check object is destroyed or not
echo is_object($obj);
?>
0 comments:
Post a Comment