Showing posts with label PHP Script. Show all posts
Showing posts with label PHP Script. Show all posts

Saturday, June 3, 2017

PHP Tag Lib: PHP Custom Tag Library (TagLib)

We use tag in your HTML page such as "DIV", "H2", "P" etc in every moment in our life. Who used Grail's or Spring MVC he has knowledge on custom tag library. We can also use custom tag lib in PHP project. Below is a example of PHP custom tag lib usage:

<tglib:upper>Goes to upper</tglib:upper>

To drive output of this custom tag in PHP, you have to download and include tag lib library first from below link:

drive.google.com/PHP Tag Lib: PHP Custom Tag Library (TagLib)

We used tglib:upper in above script, so we have to make a directory in "TagLib" directory named "upper" with a function named "tglib___upper" that support $tag parameter. With will supply tag name as well as any attributes in that tag. 

Below is a sample example of "upper" tag:

function tglib___upper($tag)
{
    return strtoupper($tag['content']);
}


$tag["content"] contains body parts of the tag.

If you download and execute "tag1.php" file, below output will be generated:





Sunday, May 28, 2017

PHP Script: Call Method Dynamically

PHP Script: Call Method Dynamically. Below is PHP Script which describes the full process:

<?php
$fnc = "canCall";
if (!is_callable($fnc)) {
    echo "Cant call method: $fnc <BR>";
} else {
    echo "Return from method $fnc: " . $fnc(15) . "<BR>";
}

$fnc = "canCall2";
if (!is_callable($fnc)) {
    echo "Cant call method: $fnc <BR>";
} else {
    echo "Return from method $fnc: " . $fnc(15) . "<BR>";
}

function canCall($a = 10)
{
    return $a * 5;
}

$caller_class = new CallerClass();
$caller_class->check();

class CallerClass
{
    private $for_method1 = 10;

    public function check()
    {
        $fnc = "method1";
        if (!is_callable(array(&$this, $fnc))) {
            echo "$fnc is not callable function";
        } else {
            $this->{"for_method1"} = 20;
            echo "Return from $fnc:" . $this->{$fnc}() . "<BR>";
        }

        $fnc = "method2";
        if (!is_callable(array(&$this, $fnc))) {
            echo "$fnc is not callable function";
        } else {
            echo "Return from $fnc:" . $this->{$fnc}() . "<BR>";
        }
    }

    public function method1()
    {
        return "From method 1 <$this->for_method1>";
    }
}

?>


Which will output as below:

Return from method canCall: 75
Cant call method: canCall2 
Return from method1:From method 1 <20>
method2 is not callable function

PHP Script: Define Variable Dynamically

PHP Script: Define Variable Dynamically. Its easy:

${"variable"} = "Variable defined dynamically";

echo $variable;


Will output:

Variable defined dynamically

And in class file (both static and instance scope):

SomeClass::test();
SomeClass::test2();

$class = new SomeClass();
$class->test3();

class SomeClass {
    static $xxx = null;

    static function test() {
        self::${"xxx"} = "Value";
    }

    static function test2() {
        echo self::${"xxx"} . "::" . self::$xxx;
    }

    function test3() {
        $this->{"yyyy"} = "Value of YYYY";
        echo "<BR>" . $this->yyyy;
    }
}