Welcome Guest, Not a member yet? Register   Sign In
sending data to library outside the array
#1

[eluser]Renato Tavares[/eluser]
Whenever I create my own library is necessary to invoke it through an array? Let me explain:

In my library I have to pass only a code of 13 digits every time I call. In the documentation there is only examples of arrays, to work around that I am using the following syntax:

Controller calling the library:

Code:
[other things....]

$object = 'SW449086657BR';
$this->load->library('correios', array($object));
$data = $this->correios->result;
$this->load->view('actions/tracking', $data);

[other things....]

receiving data from controller in library

Code:
public function __construct($object) {

        $object = strtoupper($object[0]);

        if (preg_match('/^[a-zA-Z]{2}[0-9]{9}[a-zA-Z]{2}$/', $object)) {
            $this->search($object);
        } else {
            $this->result['erro'] = 'Objeto inválido';
        }
    }
[other things....]


I did not think this the most elegant solution, it is possible to improve?

I tried to pass off an array and did not work
#2

[eluser]Renato Tavares[/eluser]
I know the question may sound stupid but have tried in various ways and it did not work.

I'm using WAMP, maybe it my environment.
#3

[eluser]boltsabre[/eluser]
Without knowing your full library, can you NOT run your constructor check in the library, but rather direct in a function, like this:
Code:
[other things....]
$this->load->library('correios');
$data = $this->correios->result('SW449086657BR');
$this->load->view('actions/tracking', $data);
[other things....]

And your library
Code:
public function result($object) {
        if (preg_match('/^[a-zA-Z]{2}[0-9]{9}[a-zA-Z]{2}$/', strtoupper($object))) {
            //do something
        } else {
           //do something else
        }
}
[other things....]




Theme © iAndrew 2016 - Forum software by © MyBB