{"id":560,"date":"2012-02-21T12:26:49","date_gmt":"2012-02-21T11:26:49","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=560"},"modified":"2012-02-24T19:30:25","modified_gmt":"2012-02-24T18:30:25","slug":"using-native-php-with-haxe-php","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/using-native-php-with-haxe-php\/","title":{"rendered":"Using native Php with haXe Php"},"content":{"rendered":"<p>Recently I had some time to dig more with haXe Php. The major question was how does it integrate with existing native Php ? I&#8217;m glad to say that it works fine!<br \/>\nLet&#8217;s start a quick overview of our native Php file test (Simple.class.php) :<\/p>\n<pre lang=\"php\" line=\"1\"><?php\r\nfunction makeSimple($text) {\r\n    return new Simple($text);\r\n}\r\n\r\nfunction affichText($text) {\r\n    echo $text;\r\n}\r\n\r\nclass Simple {\r\n    \r\n    public $text;\r\n    public $tab;\r\n    \r\n    public function __construct($text) {\r\n        $this->text = $text;\r\n        $this->tab[0] = \"index 0\";\r\n        $this->tab[1] = \"index 1\";\r\n    }\r\n    \r\n    public function doPrint() {\r\n        echo $this->text;\r\n    }\r\n    \r\n    protected function changeText($text) {\r\n        $this->text = $text;\r\n    }\r\n}\r\n\r\nclass Simple2 extends Simple {\r\n\r\n    public function __construct($text) {\r\n        parent::__construct($text);\r\n    }\r\n    \r\n    public function makeChange($text) {\r\n        $this->changeText($text);\r\n    }\r\n\r\n    public function associativeArray() {\r\n        \r\n        $tab[\"num1\"] = \"number 1\";\r\n        $tab[\"num2\"] = \"number 2\";\r\n        return $tab;\r\n    }\r\n}\r\n?><\/pre>\n<p>There are a simple function, some inheritance stuff and an associative array which is very common in Php.<\/p>\n<p>Now the haXe part :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package;\r\n\r\nimport php.Lib;\r\n\r\nclass Test {\r\n\r\n    static function main() {\r\n \r\n    \tnew Test();\r\n    }\r\n \r\n    public function new() {\r\n    \t\r\n        \/\/ import php file\r\n    \tuntyped __call__(\"require_once\", \"Simple.class.php\");\r\n\r\n        \/\/ call a php function with an arg\r\n    \tuntyped __call__(\"affichText\", \"first msg <\/br>\");\r\n\r\n        \/\/ create a php object with an arg\r\n    \tvar myPhpObject:Dynamic = untyped __call__('new Simple2', 'second msg <\/br>');\r\n\r\n        \/\/ manipulate the object\r\n    \tmyPhpObject.doPrint();\r\n    \tmyPhpObject.makeChange(\"some new text <\/br>\");\r\n    \tmyPhpObject.doPrint();\r\n\r\n        \/\/ print an array\r\n        Lib.print(myPhpObject.tab);\r\n\r\n        \/\/ trace the index 0 value\r\n        trace(myPhpObject.tab[0]);\r\n\r\n        \/\/ make some native php\r\n        untyped __php__(\"echo '<\/br>php native from haXe !<\/br>'\");\r\n\r\n        \/\/ we need a Hashtable to parse an associative array from php :\r\n        var phpAssociativeArray:Hash<String> = Lib.hashOfAssociativeArray(myPhpObject.associativeArray());\r\n\r\n        \/\/ trace the key value num2\r\n        trace(phpAssociativeArray.get(\"num2\"));\r\n    }\r\n\r\n}<\/pre>\n<p>The output log : <\/p>\n<pre>first msg\r\nsecond msg\r\nsome new text\r\n[\"index 0\", \"index 1\"]Test.hx:32: index 0\r\nphp native from haXe !\r\nTest.hx:41: number 2<\/pre>\n<p>If you are using everyday libraries\/tools written in Php, you may wrap them with haXe for more comfort. Take a look there : <a href=\"http:\/\/haxe.org\/doc\/php\/extern_libraries\" target=\"_blank\">Wrapping External PHP Libraries<\/a> &#038; <a href=\"http:\/\/haxe.org\/doc\/advanced\/magic\" target=\"_blank\">the haXe Magic<\/a>.<\/p>\n<p>Unfortunately, there isn&#8217;t lots of ressources\/tutorials for haXe Php on the website, I will update this post if I go deeper in haXe Php development. It is very pleasant to write Php this way. Give it a try!<br \/>\n<a href=\"http:\/\/haxe.org\/api\" target=\"_blank\">haXe API<\/a>.<\/p>\n<p>And because memes are fashion :<br \/>\n<a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir-300x300.jpg\" alt=\"\" title=\"boromir-haxe\" width=\"300\" height=\"300\" class=\"aligncenter size-medium wp-image-564\" srcset=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir-300x300.jpg 300w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir-150x150.jpg 150w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir-48x48.jpg 48w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/02\/boromir.jpg 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I had some time to dig more with haXe Php. The major question was how does it integrate with existing native Php ? I&#8217;m glad to say that it works fine! Let&#8217;s start a quick overview of our native Php file test (Simple.class.php) : There are a simple function, some inheritance stuff and an &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/using-native-php-with-haxe-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using native Php with haXe Php<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[70,44],"tags":[110,111,71,109,79],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/560"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":8,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":569,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions\/569"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}