Archive | PHP Magice Methods RSS feed for this section

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 :

$this->setFirstName("Jhon");


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

$this->getFirstName();


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

$obj->setFirstName('Jhon')
     ->setLastName('Regan')
     ->setAge('29');

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:

$obj->fx(
          "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:

$this->setProductId(5)
      ->setPrice(“22.99”)
      ->setShipping('Corporate')
      ->setUserId(755)
      ->setInvoiceId(100000045)
      ->formatData();


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
$payment->setXLogin('[LOGIN]')
              ->setXTranKey('[Key]')
              ->setXVersion('[Version]')
              ->setXDelimData('[DELIM_DATA'])
              ->setXDelimChar('[DELIM_CHAR]')
              ->setXRelayResponse('[RELAY_RESPONSE]')
              ->setXType(['Type'])
              ->setXMethod('[METHOD]');
// Batch - 2
$processor->setXTestRequest('TRUE');

// Batch - 3		
$processor->setShippingAddress('[Shipping_Address]')
               ->formatPaymentData();		


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.