Welcome Guest, Not a member yet? Register   Sign In
Easy Question: Handling Arrays in Custom Libraries
#1

[eluser]Unknown[/eluser]
Trying to learn CodeIgniter & PHP better at the same time.

I'm building a custom library to handle payments for a registration system I'm building in CI.

Here's my basic code in my library:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Makeapayment {

    function charge($data)
    {    
        $first_name = $this->data['first_name'];
    }

}

?>

I am loading the library/calling the class in my controller as follows:

Code:
$this->load->library('Makeapayment', $data);
$this->makeapayment->charge();

It's not working correctly. I'm sure this is an easy answer for someone. How do I accessing variables in my library from the passed array?
#2

[eluser]moodh[/eluser]
Code:
$this->load->library('Makeapayment');
$this->makeapayment->charge($data);
#3

[eluser]Unknown[/eluser]
I knew it had to be something simple - thanks!
#4

[eluser]pistolPete[/eluser]
If you load the library like this,
Code:
$this->load->library('Makeapayment', $data);

the $data array will be passed to the library's constructor.
Code:
<?php
class Makeapayment {

    var $data;

    function Makeapayment($data = array())
    {    
        $this->data = $data;
    }

    function charge($data)
    {    
        $first_name = $this->data['first_name'];
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB