Saturday, October 23, 2010

Phalanger - Basics by Example



Update 1: Porting code examples to Phalanger 3 - syntax changes on namespace use.

Continue with the Basics by Example; today's version of the post written in Phalanger (PHP) Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
<?php  
// Phalanger Basics  
namespace PhpGreetProgram;
use System\DateTime;  

[\Export]  
class Greet  
{  
    // Fields or Attributes  
    private $message;  
    private $name;  
    private $loopMessage;  
    // Getters and Setters. No Properties built-in syntax availabe (yet)  
    public function GetMessage()  
    {  
        return $this->message;  
    }  
    public function SetMessage($value)  
    {  
        $this->message = $this->Capitalize($value);  
    }  
    public function GetName()  
    {  
        return $this->name;  
    }  
    public function SetName($value)  
    {  
        $this->name = $this->Capitalize($value);  
    }  
    public function GetLoopMessage()  
    {  
        return $this->loopMessage;  
    }  
    public function SetLoopMessage($value)  
    {  
        $this->loopMessage = $value;  
    }  
    // Constructor          
    public function __construct()  
    {  
        $this->message = "";  
        $this->name = "";  
        $this->loopMessage = 0;  
    }  
    // Overloaded Constructors   
    // No Overloaded Constructors support in Phalanger          
    // Method 1  
    private function Capitalize($value)  
    {              
        // "if-then-else" statement  
        if (strlen($value) >= 1)  
        {  
            return ucwords($value);  
        }  
        else   
        {  
            return "";  
        }  
    }  
    // Method 2  
    public function Salute()  
    {  
        // "for" statement  
        for ($i = 0; $i < $this->loopMessage; $i++)  
        {  
            echo "{$this->message} {$this->name}!\n";  
        }  
    }  
    // Overloaded Method  
    // No Overloaded Methods Support in Phalanger. News methods instead.  
    // Method 2.1  
    public function Salute21($message, $name, $loopMessage)  
    {  
        // "while" statement  
        $i = 0;  
        while ($i < $loopMessage)  
        {  
            echo "{$this->Capitalize($message)} {$this->Capitalize($name)}!\n";  
            $i++;  
        }  
    }  
    // Method 2.2  
    public function Salute22($name)  
    {  
        // "switch/case" statement  
        $dtNow = DateTime::$Now;              
        switch(intval($dtNow->Hour))  
        {  
            case 6: case 7: case 8: case 9: case 10: case 11:  
                $this->message = "good morning,";  
                break;  
            case 12: case 13: case 14: case 15: case 16: case 17:  
                $this->message = "good afternoon,";  
                break;  
            case 18: case 19: case 20: case 21: case 22:  
                $this->message = "good evening,";  
                break;  
            case 23: case 0: case 1: case 2: case 3: case 4: case 5:  
                $this->message = "good night,";  
                break;  
            default:  
                $this->message = "huh?";  
                break;  
        }  
        echo "{$this->Capitalize($this->message)} {$this->Capitalize($name)}!\n";  
    }  
}  
  
// Console Program  
class Program  
{  
    public static function Main()  
    {  
        // Define variable of type Greet and instantiate. Call Constructor  
        $g = new Greet();  
        // Call Setters  
        $g->SetMessage("hello");  
        $g->SetName("world");  
        $g->SetLoopMessage(5);  
        // Call Method 2  
        $g->Salute();  
        // Call Method 2.1 and Getters  
        $g->Salute21($g->GetMessage(), "phalanger", $g->GetLoopMessage());  
        // Call Method 2.2  
        $g->Salute22("carlos");  
        // Stop and exit  
        echo "Press any key to exit...";  
        fgets(STDIN);  
        return 0;  
    }  
}  
?>

Greetings Program - Minimal
<?php
// Phalanger Basics  
use System\DateTime;  

class Greet  
{  
    // Fields or Attributes  
    private $message;  
    private $name;  
    private $loopMessage;  
    // Getters and Setters. No Properties built-in syntax availabe (yet)  
    function GetMessage()  
    {  
        return $this->message;  
    }  
    function SetMessage($value)  
    {  
        $this->message = $this->Capitalize($value);  
    }  
    function GetName()  
    {  
        return $this->name;  
    }  
    function SetName($value)  
    {  
        $this->name = $this->Capitalize($value);  
    }  
    function GetLoopMessage()  
    {  
        return $this->loopMessage;  
    }  
    function SetLoopMessage($value)  
    {  
        $this->loopMessage = $value;  
    }  
    // Constructor          
    function __construct()  
    {  
        $this->message = "";  
        $this->name = "";  
        $this->loopMessage = 0;  
    }  
    // Overloaded Constructors   
    // No Overloaded Constructors support in Phalanger          
    // Method 1  
    private function Capitalize($value)  
    {              
        // "if-then-else" statement  
        if (strlen($value) >= 1)  
        {  
            return ucwords($value);  
        }  
        else   
        {  
            return "";  
        }  
    }  
    // Method 2  
    function Salute()  
    {  
        // "for" statement  
        for ($i = 0; $i < $this->loopMessage; $i++)  
        {  
            echo "{$this->message} {$this->name}!\n";  
        }  
    }  
    // Overloaded Method  
    // No Overloaded Methods Support in Phalanger. News methods instead.  
    // Method 2.1  
    function Salute21($message, $name, $loopMessage)  
    {  
        // "while" statement  
        $i = 0;  
        while ($i < $loopMessage)  
        {  
            echo "{$this->Capitalize($message)} {$this->Capitalize($name)}!\n";  
            $i++;  
        }  
    }  
    // Method 2.2  
    function Salute22($name)  
    {  
        // "switch/case" statement  
        $dtNow = DateTime::$Now;              
        switch(intval($dtNow->Hour))  
        {  
            case 6: case 7: case 8: case 9: case 10: case 11:  
                $this->message = "good morning,";  
                break;  
            case 12: case 13: case 14: case 15: case 16: case 17:  
                $this->message = "good afternoon,";  
                break;  
            case 18: case 19: case 20: case 21: case 22:  
                $this->message = "good evening,";  
                break;  
            case 23: case 0: case 1: case 2: case 3: case 4: case 5:  
                $this->message = "good night,";  
                break;  
            default:  
                $this->message = "huh?";  
        }  
        echo "{$this->Capitalize($this->message)} {$this->Capitalize($name)}!\n";  
    }  
}  
  
// Console Program  
class Program  
{  
    static function Main()  
    {  
        // Define variable of type Greet and instantiate. Call Constructor  
        $g = new Greet();  
        // Call Setters  
        $g->SetMessage("hello");  
        $g->SetName("world");  
        $g->SetLoopMessage(5);  
        // Call Method 2  
        $g->Salute();  
        // Call Method 2.1 and Getters  
        $g->Salute21($g->GetMessage(), "phalanger", $g->GetLoopMessage());  
        // Call Method 2.2  
        $g->Salute22("carlos");  
        // Stop and exit  
        echo "Press any key to exit...";  
        fgets(STDIN);  
    }  
}  
?>

And the Output is:



Emulating Overloaded Constructor in Phalanger (PHP)
PHP and Phalanger do not support Overloading Constructors (at least not so far, in the version I'm using), but there is another way you can "emulate" that by creating one constructor with all parameters you need, but assigning a default value to them and then, validating that if you provide those parameters, you use them, otherwise, default init values will be use as shown here below.

<?
class Greet
{    # Fields or Attributes
    private $message;
    private $name;
    private $loopMessage;    
    # Constructor
    function __construct($message=false, $name=false, $loopMessage=false)
    {
        # Use Constructor parameter only if it is was supplied if not use default
        if($message)
            $this->message = $message;
        else
            $this->message = "bye";
        if($name)
            $this->name = $name;
        else
            $this->name = "carlos";
        if($loopMessage)
            $this->loopMessage = $loopMessage;
        else
            $this->loopMessage = 0;
    }        
    // Method 2
    function Salute()
    {
        echo "{$this->loopMessage} {$this->message} {$this->name}!\n";
    }
}

// Console Program
class Program
{
    static function Main()
    {
        // Call Constructor using Defaults
        $g = new Greet();
        $g->Salute();
        // Call Constructor using Parameters
        $g = new Greet("hello", "world", 5);
        $g->Salute();
        // Stop and exit
        echo "Press any key to exit...";
        fgets(STDIN);
    }
}
?>

And the Output is:

4 comments:

  1. i see this article on Phalanger - Basics by Example. it's very interesing for me. thanks for sharing.

    ReplyDelete
  2. how to install and setup with ubuntu ?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi weboceanbiz,
      Look at phalanger site for that. There is a page with the config info for mono.

      http://www.php-compiler.net/blog/2012/phalanger-php-mono#more-637

      Look at: Installing Phalanger on Linux

      If that doesn't help, post the question on phalanger's blog or facebook.

      Delete