Easy way to pass value by using Magic Methods in appRain?

12 Oct

In appRain we can avoid some unnecessary hassle to pass value using Magic Methods.

What is magic methods?
A magic method does not have a physical existence in Project code. We use this to pass value from one scope to another scope. Each magic method has a pair of two methods.
1) First method set value that always start with a keyword “set”. For example :


2) Second method retrieve value and it start with a keyword “get”. For example:


This method return value set by $this->setFirstName() and if value is not set then it return null value.

The “set” method always return reference it’s self class so that we can create a chain of call like bellow


What is the Problem of argument passing technique?
It’s really boring to pass lots of value in different scope by function parameters be cause it fixed developers in a frame. In some cases if parameters become optional then it’s taught to manage the sequence.
For example:

          "param 1" ,
           "param 2" ,
           "param n")

Let say in above example we like to skip param-1 but pass param-1 so you have to pass a default value in param-1 to maintain the sequence.

So, How does magic method help?
It’s really an amazing utility that we get form magic methods. Let say we have a function that we us to format product information, we can use magic method to set values like:


So we have create a chain of five magic methods to pass value and finally called formatData() method and in we will retrieve value like $this->getPrice() or $this->getUserId(). Now if we don’t like pass Invoice id simply you can comment it out or next time if we need to pass another value then simply we will add that another magic methods. And for the new change we don’t need to re-write function definition.

Show some Examples:
There are some good example when we save data in model like bellow:

See we can add/remove filed easily if needed. Let’s say we have create a plugin for authorize.net payment process were we have to pass bunch of parameters. We can do that simple like bellow.

$payment = App::Helper('Authorizedotnet');

// Batch - 1
// Batch - 2

// Batch - 3		

In this example we have set data in different steps and inside class we can catch it like bellow:

$post_values = array(
        "x_login"                    => $this->getXLogin(),
        "x_tran_key"               => $this->getXTranKey(),
        "x_version"	                => $this->getXVersion(),
        "x_test_request"          => $this->getXTestRequest(),
        "x_delim_data"            => $this->getXDelimData(),
        "x_delim_char"            => $this->getXDelimChar(),
        "x_relay_response"      => $this->getXRelayResponse(),
        "x_type"                     => $this->getXType(),
        "x_method"                 => $this->getXMethod(),
        "x_card_num"              => $this->getXCardNum(),
        "x_exp_date"               => $this->getXExpDate(),
        "x_amount"	                 => $this->getXAmount(),
        "x_description"             => $this->getXDescription(),
        "x_first_name"             => $this->getXFirstName(),
        "x_last_name"              => $this->getXLastName(),
        "x_address"                 => $this->getXAddress(),
        "x_state"                     => $this->getXState(),
        "x_zip"                        => $this->getXZip());

So, It’s pretty helpful to pass data or push it for a certain period.


9 Responses to “Easy way to pass value by using Magic Methods in appRain?”

  1. Kim zeebun October 12, 2010 at 1:30 pm #

    There is a basic requirement to get access of magic methods in any appRain class and that is we have to extend “appRain_Base_Objects”.

  2. Rubel October 12, 2010 at 1:39 pm #

    Yes that’s true,
    But generally all default resource is extended to that class. It’s important if we create a new class in the system like Helper or Plugin.

    It also give access on language function ($this->__()) and database abstraction layer.

  3. Tanveer Noman October 12, 2010 at 1:49 pm #

    Magic function is helpful for data passing. Basically is push data in a Global Resource class. There is an another method that help to unset Resource which is skipped by the post.

    We can use it like

    It’s really good to clear unnecessary resource.

  4. Rubel October 12, 2010 at 2:18 pm #

    @Tanveer Noman

    Thank you.. It’s really a good pick.

  5. Jon Regan October 12, 2010 at 2:22 pm #

    I have a little concern about the calling of magic function. Should I call function several time in a single scope or save it in a local variable if I need to use it multiple times.


  6. Rubel October 12, 2010 at 2:24 pm #

    I would suggest to use it from function it’s not necessary to save in a variable that will access little bit more primary memory.

  7. Tanveer Noman October 12, 2010 at 2:33 pm #

    It’s better use it ase per need for your coding help. If you save in local variable it will blog some member but it will save unnecessary function calling. So both this these methods are identical.


  8. Rubel October 12, 2010 at 2:37 pm #

    Member management is little bit more necessary then saving CPU cycle. 😀

    Well! Both of these method is good because it’s a matter of tiny resource uses. 😉

  9. Jon Regan October 12, 2010 at 2:38 pm #

    Thnx for replay. This debate will help others.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: