Login   Register  
PHP Classes
elePHPant
Icontem

File: Manual

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mike Stowe  >  smrtClass  >  Manual  >  Download  
File: Manual
Role: Documentation
Content type: text/plain
Description: PHP Manual for smrtClass
Class: smrtClass
Manage objects with variables of strict types
Author: By
Last change: Added addFree method explanation
Date: 3 years ago
Size: 4,529 bytes
 

Contents

Class file image Download
*** MANUAL ***
@author  Michael Stowe
@link    http://www.mikestowe.com/php
@version 1.0

== smrtClass ==
smrtClass is a restrictive data type class based on PHP's stdClass.  Instead of allowing the free creation and setting of properties as 
the stdClass does, smrtClass requires properties to be setup first with a specific type or regex requirement.  Data is then checked 
against this requirement when the property is modified, and if inconsistent with the data requirements throws an exception.

smrtClass is designed to provide an added layer of security to PHP applications, and while it does not provide 100% protection, it does 
signigicantly help to prevent malicious, dangerous, or malformed data from making its way into the applications crucial systems.


*** CLASS METHODS ***

== smrtClass::add ==
smrtClass::add( $var_name, $var_type = ['string'|'int'|'bool'|'object'|'array',|'date'|'datetime'|'timestamp'|'email'|'url'], $value = null );

Add a new property

$test = new smrtClass();
$test->add('number', 'int');
$test->number = '10'; // converted to integer
echo gettype($test->number): // displays "integer"

$test->add('string');
$test->string = 'Hello World'; // set as a string

$test->add('emailAddress', 'email');
$test->emailAddress = 'test@test.com'; // validated to be an email and set as string



== smrtClass::addRegex ==
smrtClass::addRegex( $var_name, $regex, $value = null );

Add a new Regex validated property

$test = new smrtClass();
$test->addRegex('numberOnly', '/^[0-9]+$/');
$test->numberOnly = 10; // valid
$test->numberOnly = 'ten'; // throws exception

$test->add('alphaOnly', '/^[a-z]$/i');
$test->alphaOnly = 'Hello'; // valid
$test->alphaOnly = 'Hello world'; // throws an exception because of the space



== smrtClass::addFree ==
smrtClass::addFree( $var_name, $value = null );

Add a new, unvalidated property that accepts any data type
Recommended only for properties with morphing data-types

$test = new smrtClass();
$test->addFree('mixed');
$test->number = 'hello world'; // 
echo gettype($test->number): // displays "hello world"

$test->number = array('one', 'two');
echo $test->number; // displays "Array"
var_dump($test->number); // array( 0 => 'one', 1 => 'two' );



== smrtClass::set ==
smrtClass:set( $var_name, $value )

Set the value of a pre-existing property

$test = new smrtClass();
$test->add('name');
$test->set('name', 'Michael');
$test->name = 'Michael'; // does the same thing as set



== smrtClass::remove ==
smrtClass:remove( $var_name )

Delete a property - alias of unset()

$test = new smrtClass();
$test->add('name');
$test->name = 'Michael';
$test->delete('name'); // removes the name property
unset($test->name); // does the same thing as remove



=== smrtClass::lock ===
smrtClass::lock()

Locks creation and removal of properties

$test = new smrtClass();
$test->add('item1');
$test->lock();
$this->item1 = 'Hello World'; // this works as data can be manipulated
$test->add('item2'); // throws an exception as properties are locked
$test->remove('item1'); // throws an exception as properties are locked



=== smrtClass::lockData ===
smrtClass::lockData()

Locks all data manipulation as well as creation and removal of properties

$test = new smrtClass();
$test->add('item1');
$test->lockData();
$this->item1 = 'Hello World'; // throws an exception as data is locked
$test->add('item2'); // throws an exception as properties are locked
$test->remove('item1'); // throws an exception as properties are locked



=== smrtClass::unlock ===
smrtClass::unlock()

Unlocks data and property creation/ removal

$test = new smrtClass();
$test->add('item1');
$test->lockData();
test->unlock();
$this->item1 = 'Hello World'; // this now works
$test->add('item2'); // this now works
$test->remove('item1'); // this now works



=== smrtClass::unlockData ===
smrtClass::unlockData()

Unlocks data while keeping property creation and removal locks in place

$test = new smrtClass();
$test->add('item1');
$test->lockData();
test->unlockData();
$this->item1 = 'Hello World'; // this now works
$test->add('item2'); // throws an exception as properties are locked
$test->remove('item1'); // throws an exception as properties are locked



== smrtClass::listTypes ==
smrtClass::listTypes()

returns an array of supported types such as string, int, bool, etc.

$test = new smrtClass();
$test->listTypes();