Results 1 to 9 of 9
Threaded View
-
29th May 2010, 04:04 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comHow to streamline objects in PHP!
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
PHP Code:$id = $root->input->validate->post->id('int')
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 Code:class Test
{
function __get($item)
{
echo $item;
}
}
$test = new Test();
$test->LiteWarezIsTheBestXD; //This will print out "LiteWarezIsTheBestXD"
This allows you to set variables in the same manner above: Example:
PHP Code: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"
PHP Code: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!
}
using the magic method __call() we can now pass in parameters aswell
PHP Code: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"
IPB, use the method instead of globalisation of vars etc..
Example of IPB
PHP Code:$salt = $this->ipsclass->converge->generate_password_salt( 5 );
peacelitewarez Reviewed by litewarez on . How to streamline objects in PHP! 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 $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 ? Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
US Govt. Objects To Megaupload Hiring Top Law Firm
By ShareShiz in forum News & Current EventsReplies: 2Last Post: 13th Apr 2012, 06:05 PM -
Google Strikes Back After MPAA Objects To Hotfile Intervention
By Smith in forum News & Current EventsReplies: 1Last Post: 24th Mar 2012, 03:05 AM -
How to add expiry date to external objects?
By Rocke in forum Technical Help Desk SupportReplies: 3Last Post: 9th May 2011, 10:57 AM -
[c#] variables, ifs, simple math and drawing objects
By jayfella in forum Web Development AreaReplies: 23Last Post: 18th Jun 2010, 04:52 PM
themaPoster - post to forums and...
Version 5.38 released. Open older version (or...