Results 1 to 9 of 9
-
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
-
29th May 2010, 04:13 PM #2Respected Member
This tutorial is terrible, all you've done is post 3 code snippets and you haven't even explained what the code does properly. Perhaps you should explain how to actually use "magic methods"? Rather then pasting the code and expecting us to figure it out automatically.
Anyway, good contribution I guess, better then nothing.
-
29th May 2010, 04:37 PM #3Respected Developer
Instead of posting tutorials would you mind helping JmZ code a few plugins for his auto
?
Nice share.
-
29th May 2010, 04:38 PM #4OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comI was in a rush to finish it, ill redo it now lool!
Join 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
-
29th May 2010, 04:43 PM #5MemberWebsite's:
eih.bz pornDDL.me sexytattoochicks.tumblr.com
-
29th May 2010, 04:56 PM #6OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comIts not exactly easy to find ways of describing advanced OOP to people who are still doning
$sql = 'SELECT * FROM table WHERE id = ' . mysql_real_escape_string($_GET['id']);
Ive updated teh post describing each magic method! and how they are used.Join 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
-
29th May 2010, 05:17 PM #7It begins...
Lite, I guess tis the wrong forum to post them tuts. You'd be better off at a php forum
-
29th May 2010, 05:19 PM #8Respected Member
Nobody is crying, shut the fuck up. I appreciate his work, I could of replied with "awesome tut" like most posts in this section but I've been honest. After following my advice his tutorial is easier to read and understand which benefits everybody.
Criticism is a good thing.
-
29th May 2010, 05:26 PM #9OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comi do agree loget
This is a webmasters forum, PHP is the largest used language no web servers today, i believe this forum is teh perfect place to post my tutorialsJoin 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
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...