Example: How to create a Class and Object in Php?
<!DOCTYPE html>
<html>
<body>
<?php
class addition //class creation
{
public $name;
public $age;
}
$obj1 = new addition(); //object creation
$obj1->name= "Robert"; //direct const value assignment
$obj1->age= 42;
echo $obj1->name; //value display
echo "<br>";
echo $obj1->age;
?>
</body>
</html>
-------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
$obj1->get_val(100,200);
?>
</body>
</html>
Output:
300
-------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
private $num1;
private $num2;
private $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
$obj1->get_val(50,75);
?>
</body>
</html>
Output:
125
Example: How to operate Php using ‘this’ keyword with Class and Object?
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
public function input()
{
$this->num1=20;
$this->num2=30;
}
public function process()
{
$this->result=$this->num1+$this->num2;
}
public function output()
{
echo "The addition result is = " . $this->result;
}
}
$obj1 = new addition();
echo $obj1->input();
echo $obj1->process();
echo $obj1->output();
?>
</body>
</html>
---------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
public function input($x,$y)
{
$this->num1=$x;
$this->num2=$y;
}
public function process()
{
$this->result=$this->num1+$this->num2;
}
public function output()
{
echo "The addition result is = " . $this->result;
}
}
$obj1 = new addition();
echo $obj1->input(100,200);
echo $obj1->process();
echo $obj1->output();
?>
</body>
</html>
---------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
public function input($x,$y)
{
$this->num1=$x;
$this->num2=$y;
}
public function process()
{
$this->result=$this->num1+$this->num2;
return $this->result;
//return $this->num1+$this->num2;
}
}
$obj1 = new addition();
echo $obj1->input(100,200);
echo $obj1->process();
?>
</body>
</html>
Example: How to check an Object belonging to a Class in Php using ‘instanceof’ keyword?
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
var_dump($obj1 instanceof addition);
//$obj1->get_val(100,200);
?>
</body>
</html>
Output:
bool(true)
---------------------------- OR ----------------------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
$obj2 = new addition();
var_dump($obj1 instanceof addition);
echo "<br/>";
var_dump($obj2 instanceof addition);
//$obj1->get_val(100,200);
?>
</body>
</html>
Output:
bool(true)
bool(true)
---------------------------- OR ----------------------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
var_dump($obj5 instanceof addition);
//$obj1->get_val(100,200);
?>
</body>
</html>
Output:
bool(false)
---------------------------- OR ----------------------------
<!DOCTYPE html>
<html>
<body>
<?php
class addition
{
public $num1;
public $num2;
public $result;
function get_val($x, $y)
{
$num1=$x;
$num2=$y;
$result=$num1+$num2;
echo $result;
}
}
$obj1 = new addition();
var_dump($obj1 instanceof addition5);
//$obj1->get_val(100,200);
?>
</body>
</html>
Output:
bool(false)
0 Comments