Archive for the ‘Phalanger’ Category

  1. Hello, Connector/NET(Phalanger) World!

    Posted on 4月 9th, 2013 by cx20

    Connector/NET(Phalanger)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Phalanger による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Phalanger + Connector/NET + MySQL)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use MySqlDataMySqlClient;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "server=localhost;user id=root;password=P@ssW0rd";
            $sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
            $con = new MySqlDataMySqlClientMySqlConnection($conStr);
            $cmd = new MySqlDataMySqlClientMySqlCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    コンパイル方法

    C:¥> phpc /pure Hello.php

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  2. Hello, ODP.NET(Phalanger) World!

    Posted on 2月 20th, 2013 by cx20

    ODP.NET(Phalanger)

    ODP.NET(Oracle Data Provider for .NET)は、.NET ベースの Oracle Database 接続用 API である。ODAC(Oracle Data Access Component)と呼ばれるパッケージに含まれる。
    .NET 環境での Oracle Database 用データプロバイダとしては、マイクロソフト社が提供する「Microsoft Oracle Client」とオラクル社が提供する「ODP.NET」があるが、現在、「Microsoft Oracle Client」はマイクロソフト社自身が非推奨としており、今後は ODP.NET の使用が推奨されている。

    データプロバイダ 説明
    System.Data.OracleClient .NET Framework Data Provider for Oracle
    Oracle.DataAccess.Client Oracle Data Provider for .NET

    ソースコード(Phalanger + ODP.NET + Oracle)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use OracleDataAccessClient;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "Data Source=ORCL;User ID=scott;Password=tiger";
            $sqlStr = "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL";
     
            $con = new OracleDataAccessClientOracleConnection($conStr);
            $cmd = new OracleDataAccessClientOracleCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    コンパイル方法

    C:¥> phpc /pure Hello.php

    実行結果

    MESSAGE
    ---------------------
    Hello, ODP.NET World!
  3. Hello, ADO.NET(Phalanger) World!

    Posted on 2月 19th, 2013 by cx20

    ADO.NET(Phalanger)

    ADO.NET(ActiveX Data Objects .NET)は、ADO の後継で .NET ベースの DBMS 接続用 API である。
    .NET データプロバイダを介することで様々な DBMS への接続が可能となっている。
    .NET Framework 標準で使用できる .NET データプロバイダとしては、以下のようなプロバイダがある。

    データプロバイダ名 説明
    System.Data.SqlClient Microsoft SQL Server
    System.Data.OleDb OLE DB
    System.Data.Odbc ODBC
    System.Data.OracleClient Oracle

    ソースコード(Phalanger + ADO.NET + OLE DB + Jet データベース)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use SystemDataOleDb;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb";
            $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
            $con = new SystemDataOleDbOleDbConnection($conStr);
            $cmd = new SystemDataOleDbOleDbCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    ソースコード(Phalanger + ADO.NET + OLE DB + ACE データベース)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use SystemDataOleDb;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\hello.accdb";
            $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
            $con = new SystemDataOleDbOleDbConnection($conStr);
            $cmd = new SystemDataOleDbOleDbCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    ソースコード(Phalanger + ADO.NET + SQL Server)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use SystemDataSqlClient;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd";
            $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
            $con = new SystemDataSqlClientSqlConnection($conStr);
            $cmd = new SystemDataSqlClientSqlCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    ソースコード(Phalanger + ADO.NET + Oracle)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use SystemDataOracleClient;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "Data Source=ORCL;User ID=scott;Password=tiger";
            $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL";
     
            $con = new SystemDataOracleClientOracleConnection($conStr);
            $cmd = new SystemDataOracleClientOracleCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    ソースコード(Phalanger + ADO.NET + ODBC + MySQL)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use System\Data\Odbc;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd";
            $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
            $con = new System\Data\Odbc\OdbcConnection($conStr);
            $cmd = new System\Data\Odbc\OdbcCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                System\Console::WriteLine( $reader->GetName(0) );
                System\Console::WriteLine( "---------------------" );
                System\Console::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    コンパイル方法

    C:¥> phpc /pure Hello.php

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!
  4. Hello, Win32 API(Phalanger) World!

    Posted on 6月 22nd, 2012 by cx20

    Win32 API(Phalanger)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    Phalanger には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(Phalanger)

    <?
    use System;
    use Win32Lib;
     
    $win32 = new Win32LibWin32();
    $win32->MessageBox( new SystemIntPtr(0), "Hello, Win32 API(Phalanger) World!", "Hello, World!", 0);
    ?>

    コンパイル方法(Phalanger)

    C:¥> phpc /lang:pure /r:Win32Lib.dll Hello.php

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Phalanger) World!
    ---------------------------
    OK   
    ---------------------------
  5. Hello, Windows Forms(Phalanger) World!

    Posted on 6月 12th, 2012 by cx20

    Windows Forms(Phalanger)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は Phalanger における Windows フォーム の例となっている。

    ソースコード

    <?
    use System;
    use SystemWindowsForms;
    use SystemDrawing;
     
    class HelloForm extends SystemWindowsFormsForm
    {
        public function __construct()
        {
            $this->Size = new SystemDrawingSize( 640, 480 );
            $this->Text = 'Hello, World!';
     
            $label1 = new SystemWindowsFormsLabel();
            $label1->Size = new SystemDrawingSize( 320, 20 );
            $label1->Text = 'Hello, Windows Forms(Phalanger) World!';
            $this->Controls->Add( $label1 );
        }
        static function Main()
        {
            FormsApplication::Run(new HelloForm());
        }
    }
    ?>

    ソースコード(アプリケーション設定ファイル)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
            </classLibrary>
        </phpNet>
    </configuration>

    実行方法

    C:¥> phpc /pure /target:winexe Hello.php

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(Phalanger) World!    |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, Phalanger World!

    Posted on 1月 24th, 2012 by cx20

    Phalanger

    Phalanger は .NET 環境向けの PHP コンパイラである。名前は「PHP language compiler」の略。
    PHP の構文が使える他、.NET Framework のライブラリが使用できる。
    C# 同様に、UNIX 環境向けの .NET Framework 互換プロジェクト「Mono」により他の OS でも動作できるようになってきている。

    ソースコード

    <?php
    echo "Hello, Phalanger World!\n";
    ?>

    ソースコード(.NET ライブラリを使用した場合)

    <?php
    SystemConsole::WriteLine("Hello, Phalanger World!");
    ?>

    コンパイル方法

    C:¥> phpc hello.php

    コンパイル方法(.NET Framework を使用する場合)

    C:¥> phpc /lang:clr /r:mscorlib hello.php

    実行結果

    Hello, Phalanger World!