heya guys, Thought i would just do a quick tutorial on how to streamline objects in PHP!
Streamlining objects creates a more organized code structure! For example
Now what this is doing is asking the $root object for the input object, and then we ask the input to give us the validate object, etc etc!
So how you do this ?
You use what we call the Magic Methods in PHP Objects
The magic methods are reserved functions in php, The functions are described below!
__get
This allows you to the main streamlining: Example.
__set
This allows you to set variables in the same manner above: Example:
Ok so combining these to items will allow us to do some pretty sweet things like so!
Now let me finish of with describing how username('trim','strlen') Works:
using the magic method __call() we can now pass in parameters aswell
Now with the above example we can use $name to fetch a value from the class or w.e and then look threw the arguments to perform the function on the value before returned!
IPB, use the method instead of globalisation of vars etc..
Example of IPB
Hope you enjoyed :D
peace
Streamlining objects creates a more organized code structure! For example
PHP:
$id = $root->input->validate->post->id('int')
Now what this is doing is asking the $root object for the input object, and then we ask the input to give us the validate object, etc etc!
So how you do this ?
You use what we call the Magic Methods in PHP Objects
The magic methods are reserved functions in php, The functions are described below!
__get
This allows you to the main streamlining: Example.
PHP:
class Test
{
function __get($item)
{
echo $item;
}
}
$test = new Test();
$test->LiteWarezIsTheBestXD; //This will print out "LiteWarezIsTheBestXD"
__set
This allows you to set variables in the same manner above: Example:
PHP:
class Test
{
$items = array();
function __set($key,$value)
{
echo 'adding ' . $key . ' to $items with the value of ' . $value;
$this->items[$key] = $value;
}
}
$test = new Test();
$test->SomeKey = 'Some Value'; //This will print out "adding SomeKey to $items with the value of Some Value"
Ok so combining these to items will allow us to do some pretty sweet things like so!
PHP:
class Root
{
var $objects = array(); //Where we store other objects
function __set($objectname,$object)
{
$this->objects[$objectname] = $object;
}
function __get($objectname)
{
return $this->objects[$objectname];
}
}
//Another Class
class Input
{
var $validator;
function __construct()
{
$this->validator = new Validator();//This is a class that valdiates input
}
function __get($objectname)
{
switch($objectname)
{
case 'validator': return $this->validator; break;
}
}
}
$root = new Root(); //Create a base instance
$root->input = new Input(); //Add the input to the root object
/*
Now our base class (root) containes one object called input so we can get that at anytime by going
*/
$root->input;
/*
but because root returns the input class, thats what we are now dealing with so we then can ask the input for validator function
*/
$root->input->validate;
/*
Lets say in our validator class we had a class called post, witch is whats used to sanitize the post data, we then canm call that from validator aswell
*/
$root->input->validate->post;
/*
All the weay down to you get to your final object in witch then you use its methods
*/
if($root->input->validate->post->username('trim','strlen') < 0)
{
//This is an example of what you can make using this style of programming!
}
Now let me finish of with describing how username('trim','strlen') Works:
using the magic method __call() we can now pass in parameters aswell
PHP:
class Test
{
function __call($name,$arguments)
{
echo $name . ' Was called with the following parameters ' . implode(',',$arguments);
}
}
$test = new Test();
$test->execute('startup','process','shutdown');
// will print out " execute Was called with the following parameters startup,process,shutdown"
Now with the above example we can use $name to fetch a value from the class or w.e and then look threw the arguments to perform the function on the value before returned!
IPB, use the method instead of globalisation of vars etc..
Example of IPB
PHP:
$salt = $this->ipsclass->converge->generate_password_salt( 5 );
Hope you enjoyed :D
peace