Using TideSDK with PHP - gladenko/TideSDK GitHub Wiki

Overview

This guide demonstrates how to use the PHP module of TideSDK. The PHP module allows you to run PHP code from within your application. The syntax for Titanium Desktop 1.1 and earlier as well as the newer Titanium Desktop 1.2.0.RC1 is provided for reference. You should be using the lastest version of TideSDK in your work since Titanium Desktop is no longer supported.

For details of how to call modules for Python and Ruby, see the TLMT example in our github Documentation-Examples repository.

Including PHP

There are a few ways to include PHP scripts in your App files.

The PHP module for each platform is bundled with PHP 5.3.1, and so your users are not required to install any extra dependencies to use your PHP code, but be aware that PHP 4 is not supported.

Embedded PHP

To embed a php script in a within an html file, use a script tag with text/php type.

<script type=text/php>
function my_php_function($item){
  $result = array();
  for ($index = 0; $index < $item->count(); $index++){
    $result[] = $item[$index] + 2;
  }
  return $result;
}
</script>

<script>
alert(my_php_function([1, 2, 3, 4]));
</script>

External PHP

Including External PHP for Titanium Desktop SDKs 1.1.0 and earlier

If you have a PHP source file that you want to include there are two ways to accomplish this:

<!-- method one -->
<script type="text/php" src="my_file.php"/>

<!-- method two -->
<script type="text/php">
include("my_other_file.php");
</script>

External PHP code has full access to the DOM and the Titanium object as usual. There is one important difference between the above approaches, however. When including PHP via the {{include}} or {{require}} commands, you should surround your code with <?php ?> tags but, conversely, when including via <script> tags, you should not surround it with <?php ?> tags.

Including External PHP for Titanium Desktop >= 1.2.0.RC1 SDKs and TideSDK

Note: At the time of writing, TideSDK 1.2.1 is at a pre-release phase of development and, for this reason, is not officially supported.

With Titanium Desktop 1.2.0.RC1 and TideSDK onwards, the syntax for calling PHP from javascript has changed. We now have a Titanium.include() function that aligns the Desktop API with the mobile SDKs.

The following is an example of the new syntax:

In your index.html

<html>
<head>
<title>Titanium Language Module Tester</title>
<script>
  //Test php include
  Titanium.include("php.php");
</script>

<script>
  //We should be able to call a method from any of these three language files now
  alert("PHP TEST: " + my_php_function([1, 2, 3, 4]));
</script>
<script>
  function use_my_php_data(val) {
    window.alert(val)
  }
</script>
</head>
<body style="background-color:#1c1c1c;margin:0">
 <div style="border-top:1px solid #404040">
   <div style="color:#fff;padding:10px">
     Welcome to the Titanium Language Module Tester
   </div>
 </div>
</body>
</html>
<script>
  var globalObject = new Object();
  globalObject.foo = "string";
</script>
<script type="text/php">
  $globalObject->foo = "string2"; # globalObject will change

  function changeNumber($obj) {
  # obj is passed by reference so globalObject will change
    $obj->foo = "string3";
  }
  changeNumber($globalObject)
</script>

PHP objects work the same way when moving to JavaScript. Here's an example:

<script type="text/php">
  class MyObject {
    public $publicVariable;

    function __construct(){
      $this->publicVariable = "bar";
    }

    public function publicMethod() {
      return "foo";
    }
  }
  $globalObject = new MyObject()
</script>

<script>
  alert(globalObject.publicVariable);
  alert(globalObject.publicMethod());

  globalObject.publicVariable = "propagain";
  alert(globalObject.publicVariable);
</script>

PHP arrays in JavaScript

PHP arrays, when passed to JavaScript, are Objects whose properties are just dict accesses. For instance:

<script type="text/php">
  $globalObject = array();
  $globalObject['foo'] = "string";
</script>

<script>
  alert(globalObject.foo);
</script>

JavaScript arrays in PHP

JavaScript arrays in PHP are wrapped in an object similar to an ArrayObject

<script>
  var globalArray = [1, 2, 3, 4];
</script>

<script type="text/php">
  $globalArray->append(3);
  $globalArray->append(4);
  $window->alert($globalArray[0]);
</script>

Examples

JQuery + Titanium + PHP fun

Thanks to funkatron for this nice mind blowing example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>
<body>
  <div style="border-top:1px solid #404040">
    <div style="padding:10px">
      Welcome to Titanium
    </div>
  </div>

  <form>
    <input type="button" name="invokePHP"
    value="Fade-in with PHP" id="invokePHP"/>
  </form>

  <div id="phpjQ" style="display:none; background-color:black; padding:10px">
    This is PHP controlling jQuery
  </div>

  <script type="text/php">
    $jQuery()->ready( function () use (&$jQuery) {
      $jQuery('#invokePHP')->live('click', function () use (&$jQuery) {
        $jQuery('#phpjQ')->css('color', 'red')->fadeIn(2000);
      });
    });
  </script>
</body>
</html>

Known Issues

Known Issues for Titanium Desktop SDK 1.1.0 and earlier

  • using echo to send output to the DOM doesn't seem to work

  • Top-level classes can be accessed in <script> tags, but they must be prefixed by a top-level namespace specifier

    <script>
      $x = new SimpleXMLElement($someText);
    </script>
    
  • {{require_once}} does not seem to work properly. Tinu Coman suggested this workaround:

    if(!class_exists('MyClass')) {
      include "class1.php";
      include "file2.php";
      include "file3.php";
      include "file4.php";
    }
    

Known Issues for Titanium Desktop SDK 1.2.0.RC1, TideSDK 1.2.1RC1 and Later

  • using echo to send output to the DOM doesn't seem to work

Copyright and Attribution

The following copyright and attribution applies to this document:

  • Copyright © 2012 Appcelerator Inc. All rights reserved.
  • Copyright © 2012 David Pratt (for TideSDK). All rights reserved.

CONTRIBUTORS:

  • David Pratt
⚠️ **GitHub.com Fallback** ⚠️