PHPStrongV is a lightweight PHP library that provides object-oriented, method driven and (optionally) strongly typed common data types for PHP. It is inspired from .NET primitive type implementation. In addition, it boasts a Key-Value Dictionary implementation, a dynamically adjusted List implementation as well as LIFO Stack and FIFO Queue implementations. PHPStrongV is a developer's swiss army knife when working with data, providing efficiency, code clearness and optional strong type safety.
How It Works
PHPStrongV implements a class for each data type it provides, providing special methods to manipulate its data. For example, when creating a PHPStrongV based string variable $v, you instantiate an instance of SVString class. The variable is an instantiated SVString class on which you may call methods to manipulate it such as $v->toUpper() etc.
PHP 5.3 or newer
- Copy the lib directory in your project under a directory of your choice.
- Include the PHPStrongV.php file, which is located in the lib directory.
- Call the appropriate svX function as shown in the Examples section to create variables of different types.
/*** Create a strongly typed string with value 'test' ***/
$var_string = &svstring('test');
/*** Create a weakly typed string with value 'test' ***/
$var_string = svstring('test');
/*** Change the variable's value ***/
$var_string = 'test 2'; // or
/*** Get the variable's value. returns 'test 2' ***/
$value = $var_string->val();
/*** Make the value uppercase ***/
$value = $var_string->toUpper();
/*** Create an integer 32 with value 10 ***/
$var_int = &svint(10);
/*** Get the value of integer 32 ***/
$var_int->val(); // 10
/*** Will throw an exception as '2' is string and not an integer ***/
$var_int32 = '2';
/*** Will throw an exception as value is out of bounds ***/
$var_int32 = 9223372036854775807;
$var_int32 = 2; // correct
/*** Create a new list which accepts strings ***/
$var_list = &svlist('string');
// the list now has the following elements in the order presented:
// element1, element2, element3
// now let's reverse the order of the elements in the list:
// we read the element at index 0 which is 'element3'
$var_element = var_list->at(0);
/*** Create a new dictionary which accepts strings ***/
$var_dic = &svdictionary('string');
$var_value = $var_dic->at('key1'); // will return 'value1'
$var_dic->add('key2', 1); // will throw an exception as 1 is not a string.