Skip to content
WJunction - Webmaster Forum

How to streamline objects in PHP!

Status
Not open for further replies.
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:
$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
 

8 comments

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. ^o)
 
Instead of posting tutorials would you mind helping JmZ code a few plugins for his auto :P ?

Nice share.
 
Its 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.
 
You guys always give criticism. Stop crying and make a better tut? Appreciate others work -_-

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. (y)
 
i do agree loget :D

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 tutorials :D
 
Status
Not open for further replies.

About the author

L
Active Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom