Class QType
Type Library to add some support for strongly named types.
PHP does not support strongly named types. The QCubed type library and QCubed typing in general attempts to bring some structure to types when passing in values, properties, parameters to/from QCubed framework objects and methods.
The Type library attempts to allow as much flexibility as possible to set and cast variables to other types, similar to how PHP does it natively, but simply adds a big more structure to it.
For example, regardless if a variable is an integer, boolean, or string, QType::Cast will allow the flexibility of those values to interchange with each other with little to no issue.
In addition to value objects (ints, bools, floats, strings), the Type library also supports object casting. While technically casting one object to another is not a true cast, QType::Cast does at least ensure that the tap being "casted" to is a legitamate subclass of the object being "cast". So if you have ParentClass, and you have a ChildClass that extends ParentClass, $objChildClass = new ChildClass(); $objParentClass = new ParentClass(); Type::Cast($objChildClass, 'ParentClass'); // is a legal cast Type::Cast($objParentClass, 'ChildClass'); // will throw an InvalidCastException
For values, specifically int to string conversion, one different between QType::Cast and PHP (in order to add structure) is that if an integer contains alpha characters, PHP would normally allow that through w/o complaint, simply ignoring any numeric characters past the first alpha character. QType::Cast would instead throw an InvalidCastException to let the developer immedaitely know that something doesn't look right.
In theory, the type library should maintain the same level of flexibility PHP developers are accostomed to, while providing a mechanism to limit careless coding errors and tough to figure out mistakes due to PHP's sometimes overly laxed type conversions.
final public
|
#
__construct( )
This faux constructor method throws a caller exception. The Type object should never be instantiated, and this constructor override simply guarantees it. |
public static
integer
|
#
SetBehaviour( integer $intBehaviour )
This method can be used to change the casting behaviour of QType::Cast(). By default QType::Cast() does lots of validation and type casting (using settype()). Depending on your application you may or may not need validation or casting or both. In these situations you can set the necessary behaviour by passing the appropriate constant to this function. |
final public static
mixed
|
#
Cast( mixed $mixItem, string $strType )
Used to cast a variable to another type. Allows for moderate support of strongly-named types. Will throw an exception if the cast fails, causes unexpected side effects, if attempting to cast an object to a value (or vice versa), or if an object is being cast to a class that isn't a subclass (e.g. parent). The exception thrown will be an InvalidCastException, which extends CallerException. |
final public static
string
|
|
final public static
|
|
final public static
string
|
string |
String
String Type |
#
'string'
|
string |
Integer
Integer Type |
#
'integer'
|
string |
Float
Float Type |
#
'double'
|
string |
Boolean
Boolean Type |
#
'boolean'
|
string |
Object
Object Type |
#
'object'
|
string |
ArrayType
Array Type |
#
'array'
|
string |
DateTime
QDateTime type |
#
'QDateTime'
|
string |
Resource
Resource Type |
#
'resource'
|
string |
Association
|
#
'association'
|
string |
ReverseReference
|
#
'reverse_reference'
|
integer |
NoOp
|
#
1
|
integer |
CheckOnly
|
#
2
|
integer |
CastOnly
|
#
3
|
integer |
CheckAndCast
|
#
4
|