PHP Classes
elePHPant
Icontem

TypeSafeStruct: Manipulate type safe objects

Recommend this page to a friend!
  Info   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2009-04-13 (7 years ago) RSS 2.0 feedNot enough user ratingsTotal: 234 All time: 7,535 This week: 916Up
Version License PHP version Categories
struct 0.1GNU General Publi...5.0XML, PHP 5, Databases, Data types
Description Author

A Case Study for getting PHP type safe.

This package can be used to manipulate type safe objects in a way that works with PHP caching extensions.

It provides base classes that can check whether the types of the values to assign variables of implementation subclasses are of the expected types.

The expected types are determined by the prefixes of private variables, so it works well even when using PHP compiled code cached extensions.

An additional class can serialize objects as XML documents or to insert them in database tables.


The goal of this class is not to replace ORM or other db classes. It shall replace
the stdClass when using mysql_fetch_object.

It returns a value type object for each column. There are a number of value types:
+ TString
+ TNumber
+ TBit
+ TEnum
+ TLob
+ TTimestamp
These value types have specific sets of methods. i.e. TTimestamp value types are working
internally with the php native DateTime class.
TString has a bunch of methods to manipulate the output string.

All value type method are fluently designed.





v0.2:
- introducing ValueType Objects:
+ TString (published)
+ TNumber (published)
+ TBit (published)
+ TEnum (published)
+ TLob (published)
+ TTimestamp (published)

C# like data objects.


SAMPLE:

class Model_City extends TypeSafeStruct {

private $int_ID;
private $char_Name;
private $char_CountryCode;
private $char_District;
private $int_Population;

public function __set($key,$value) {
$type = $this->getPropertyType($key);
if($type and $this->hasProperty($key)) {
$this->{$type."_".$key} = $value;
} else {
$this->{"set".$key}($value);
}
}

public function __get($key) {
return $this->{$key};
}

}


$link = mysql_connect('localhost', 'root', 'pwd');
if (!$link) die('no connection established: ' . mysql_error());

$db = mysql_select_db('dbase', $link);
if (!$db)die ('no able to use db: ' . mysql_error());

$result = mysql_query("SELECT * FROM City LIMIT 4;");
if(!$result) die("no result set: ". mysql_error());

while($row=mysql_fetch_object($result, "Model_City")){
echo $row->getName()->toUpper()->padRight(15);
echo $row->getCountryCode()->toUpper()->padRight(5);
echo $row->getDistrict()->toUpper()->padRight(15);
echo $row->getPopulation()->format(0,",",".");
echo "\n";
}

mysql_close($link);

Innovation Award
PHP Programming Innovation award nominee
April 2009
Number 11
PHP 5 provides type hinting support that allows verifying the types of values passed to class function parameters, but it only supports checking parameters that expect object or array parameters.

This package provides an alternative solution for checking parameters of setter functions for many other types of values. It is based on the name of function that is called.

Manuel Lemos
Picture of Tom Schaefer
  Performance   Level  
Name: Tom Schaefer <contact>
Classes: 39 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 9x

Winner: 1x

Details
There are some attempts to build type safe php objects, but they all have lacks on working with eaccelarator, when they check against phpdoc comments as type safety validation notation. 
E.g. POPO (plain old php objects) from http://jan.kneschke.de/projects/typesafe-objects-in-php validates 
against the doc comment of the reflected object. But when the object is bytecoded by eAccelarator all comments 
are removed from the source. Means: "type safe" properties loose their information and the object does not work any more.

This class set is an attempt to fix this lack.
Look at the sample (auto-generated db model struct class file) and see how it works.

All object properties have their own dynamic getters and setters. So, if you have a db column named caption, then you can set a value by simply writing (when the object is instantiated): $o->setCaption("my value").

Properties of a model class do only need a type prefix in front of the property name. Supported type prefixes for now:
- string (varchar, char, text, etc.)
- float (real,decimal,float)
- integer (int,smallint,bigint,etc.)
- bool (tinyint,bool)
- array (enum)


How do I insert a value?

Valid setValue sample:
$clsObject = new Model_Objects_Countries;
$clsObject->setContinent("Asia");

Invalid setValue sample:
$clsObject = new Model_Objects_Countries;
$clsObject->setContinent(true);

If you try to set an invalid value then an exception will thrown



How do I get a value?

$clsObject = new Model_Objects_Countries;
$clsObject->getContinent();


Once an object is filled, you can use the SerializeTypeStruct class
to convert your object into different output formats.

SerializeTypeStruct provides only two formats for giving you
an image of what can be done.

You can implement any kind of output format (CSV, PDF, XLS and so on), but you have to write it on your own.

Regards Tom
  Files folder image Files  
File Role Description
Files folder imagever0.2 (10 files)
Plain text file TypedStruct.class.php Class Base class
Plain text file SerializeTypedStruct.class.php Class Sample Base class extension => serializes an object's properties
Plain text file TypeSafeStruct.class.php Class Base class extension
Accessible without login Plain text file sample.php Example as the name says
Accessible without login Plain text file baseobject_country.php Example persistent type safe model object operating with mysql sample world db
Accessible without login Plain text file readme.txt Doc. readme

  Files folder image Files  /  ver0.2  
File Role Description
  Accessible without login Plain text file sample.php Example sample console script running sql select
  Accessible without login Plain text file sampledate.php Example Working with timestamps
  Plain text file TBit.class.php Class ValueType Object
  Plain text file TEnum.class.php Class ValueType Object
  Plain text file TLob.class.php Class ValueType Object
  Plain text file TNumber.class.php Class ValueType Object
  Plain text file TString.class.php Class ValueType Object
  Plain text file TTimestamp.class.php Class ValueType Object
  Plain text file TypedStruct.class.php Class Typed Struct
  Plain text file TypeSafeStruct.class.php Class Type Safe Struct

Downloadstruct-2009-04-13.zip 16KB
Downloadstruct-2009-04-13.tar.gz 11KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
TString Download .zip .tar.gz since v0.2 TSS will support ValueType Objects. TString is the first kind of value type. Optional
TNumber Download .zip .tar.gz Value Type Object Optional
TBit Download .zip .tar.gz ValueTypeObject Optional
 Version Control Unique User Downloads Download Rankings  
 0%
Total:234
This week:0
All time:7,535
This week:916Up