PHP Classes

File: autoboxExample.php

Recommend this page to a friend!
  Classes of Artur Graniszewski   Java and C# compatible object autoboxing for PHP   autoboxExample.php   Download  
File: autoboxExample.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Java and C# compatible object autoboxing for PHP
Wrap string and integer values in objects
Author: By
Last change: Another fix in code comments: "// now try to change the data type to PHP Integer:: " was in the wrong place of code...
Date: 13 years ago
Size: 4,336 bytes
 

Contents

Class file image Download
<?php

/**
 * JAVA Autoboxing (part of Lotos Framework)
 *
 * Copyright (c) 2005-2010 Artur Graniszewski (aargoth@boo.pl)
 * All rights reserved.
 *
 * @category Library
 * @package Lotos
 * @subpackage DataTypes
 * @copyright Copyright (c) 2005-2010 Artur Graniszewski (aargoth@boo.pl)
 * @license GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
 * @version $Id$
 */

 
// load required library
include("./variablesManager.php");

// and an example classes
include("./autoboxExampleClasses.php");


// create a new String object (see autoboxExampleClass.php for class definition)
$text = & string("Initial text");

echo
'The initial value of $text variable is "'.$text.'"<br />';
echo
'The initial datatype of $text variable is '.gettype($text).(is_object($text) ? ' ('.get_class($text).')': '').'<br />';

// ------------------------
echo "<hr />";

echo
'Now we are changing $text value to 12 (integer)<br />';

// now try to change the data type to PHP Integer:
$text = 12;

// from this moment $text should be an integer and not a String object (this is a default PHP behaviour),
// but thanks to our autoboxing, this isn't true. $text is still an object
echo 'The new value of $text variable is "'.$text.'"<br />';
echo
'The new datatype of $text variable is '.gettype($text).(is_object($text) ? ' ('.get_class($text).')': '').'<br />';

// ------------------------
echo "<hr />";

echo
'Now we are changing $text value to "autoboxing test" (plain PHP string)<br />';

// now try to change the data type to PHP plain string (not an object)::
$text = "autoboxing test";

// from this moment $text should become a plain string and not a String object (this is a default PHP behaviour),
// but thanks to our autoboxing, this isn't true. $text is still an object
echo 'The new value of $text variable is "'.$text.'"<br />';

// NOTE: now we are operating on $text like on any other object (using $text->toUpperCase()) - just like in JAVA anc C#
// without enabled autoboxing PHP would raise a FATAL ERROR in the next line of code:
echo 'The new uppercased value of $text variable is "'.$text->toUpperCase().'"<br />';
echo
'The new datatype of $text variable is '.gettype($text).(is_object($text) ? ' ('.get_class($text).')': '').'<br />';

// ------------------------
echo "<hr />";

echo
'The next test is to create new String object and overwrite last one with the new one<br />';

// now try to change the data type to another String object
$text = & string("New text object");

echo
'The new value of $text variable is "'.$text.'"<br />';

// NOTE: now we are operating on $text like on any other object (using $text->toUpperCase()) - just like in JAVA anc C#
echo 'The new uppercased value of $text variable is "'.$text->toUpperCase().'"<br />';
echo
'The new datatype of $text variable is '.gettype($text).(is_object($text) ? ' ('.get_class($text).')': '').'<br />';


// ------------------------
echo "<hr />";

echo
'This time we are overwritting $text with an object of the different type (other than String class)<br />';

// now try to change the data type to stdClass object
$text = new stdClass();

echo
'The new datatype of $text variable is '.gettype($text).(is_object($text) ? ' ('.get_class($text).')': '').'<br />';

echo
'Ofcourse unset() works also, autoboxed objects can be destroyed or overriden at any time by the PHP developer just like in a plain old PHP';


// ------------------------
echo "<hr />";

echo
'And finally some technical info from Lotos Variables Manager (used by autoboxing caching mechanism)<br />';

// now try some caching...
 
$text = & cachedString("test");
$text2 = & cachedString("test2");
$text3 = & cachedString("lorem ipsum");
$text4 = & cachedString("lorem ipsum");
$text5 = & cachedString("lorem ipsum");

echo
'<pre>';
echo
'<pre>After creating 2 unique texts and 3 copies of "lorem ipsum":<br />';
print_r(VariablesManager::getPoolUsage());
echo
'</pre>';




echo
'<pre>After deletion of $text5:<br />';
$text5 = "";
print_r(VariablesManager::getPoolUsage());
echo
'</pre>';

echo
'<pre>After deletion of $text4 and $text3:<br />';
$text4 = "";
$text3 = "";
print_r(VariablesManager::getPoolUsage());
echo
'</pre>';


echo
"<hr>";