Conditional Functions

 <?php

//All Conditional Functions will give two result : True OR False
// class_exists(), interface_exists(), method_exists(), trait_exists(), property_exists(), is_a(),
//  is_subclass_of() are Conditional Functions in PHP
class MyClass
{  
    public $test;
    public function myMethod()
    {}
}
$object = new MyClass();
interface MyInterface
{}

trait myTrait
{}

//  Checking Whether any Object is of any particular Class OR Not
if(is_a($object, "MyClass"))
{
    echo "This Object is of Class MyClass" . "<br>";
}
else
{
    echo "This Object is Not of Class MyClass" . "<br>";
}

//  Checking for the Existence of Property
if(property_exists("MyClass","test"))
{
    echo "This property is Exists" . "<br>";
}
else
{
    echo "This property is Not Exists" . "<br>";
}

//  Checking for the Existence of Trait
if(trait_exists("myTrait"))
{
    echo "This Trait is Exists" . "<br>";
}
else
{
    echo "This Trait is Not Exists" . "<br>";
}

// Checking for the Existence of Method
if(method_exists($object, "myMethod"))
{
    echo "This Method is Exists" . "<br>";
}
else
{
    echo "This Method is Not Exists" . "<br>";
}

//  Checking for the Existence of Class
if(class_exists("MyClass"))
{
    echo "This Class is Exist" . "<br>";
}
else
{
    echo "This Class is Not Exist" . "<br>";
}

//  Checking for the Existence of Interface
if(interface_exists("MyInterface"))
{
    echo "This interface is Exist" . "<br>";
}
else
{
    echo "This interface is Not Exists" . "<br>";
}

class ParentClass
{}
class ChildClass extends ParentClass
{

}
$obj = new ChildClass();

//  Checking any Object is of any childClass of ParentClass OR Not
if(is_subclass_of($obj, "ParentClass"))
{
    echo "This \$obj is a object of childclass of parentclass" . "<br>";
}
else
{
    echo "This \$obj is Not a object of childclass of parentclass" . "<br>";
}

//  OUTPUT:
//This Object is of Class MyClass
// This property is Exists
// This Trait is Exists
// This Method is Exists
// This Class is Exist
// This interface is Exist
// This $obj is a object of childclass of parentclass

?>

Comments

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database