0

New to PHP, oop & codeigniter. My CI version is 3.1.2.

I am trying to load a library that I built, trying to understand Object oriented principles. Probably I'm missing something.

Below is my library.

<?php
class DPS {
    private $mode;

    public function __construct($params){
        //print_r($params);
        $this->mode = $params['mode'];
    }   

    public function get_url(){
        if($this->$mode == "TEST"){
            return "https://uat.paymentexpress.com/pxaccess/pxpay.aspx";
        } elseif($this->$mode == "LIVE"){
            return "https://sec.paymentexpress.com/pxaccess/pxpay.aspx";
        }
    }
}
?>

I'm trying to access the get_url method in the controller below.

$params = [ 'mode' => 'TEST'];
$dps = $this->load->library('DPS',$params);     
echo $dps->get_url();

Codeigniter is throwing the below error message.

Fatal error: Call to undefined method CI_Loader::get_url()

Being a newbie I'm probably missing some sort of OO fancy thingi. I just can't figure it out. Any help is greatly appreciated.

Thank you in advance

2 Answers 2

1

Try this,

$this->load->library('dps',$params);     
echo $this->dps->get_url();
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I get "Message: Undefined variable: mode" error when I try that
oops, you should make small letters instead caps, while loading library, check my edited answer. @ElaBuwa
I get 2 errors. 1. Message: Undefined variable: mode -> pointed to the library. 2. Message: Cannot access empty property -> pointed to the library. All errors are pointed to the line that reads "if($this->$mode == "TEST"){"
change both lines that says $this->$mode to $this->mode
0

Always load libraries in Codeigniter like this

$this->load->library('library_name');

And call to method of library using

$this->library_name->method_name();

SO try like this...

Save your library in application/libraries as Dps.php.Then

$params = [ 'mode' => 'TEST'];
$dps = $this->load->library('dps',$params);     
echo $this->dps->get_url();

And In your DPS library. Correct it

public function get_url(){
        if($this->mode == "TEST"){
            return "https://uat.paymentexpress.com/pxaccess/pxpay.aspx";
        } elseif($this->mode == "LIVE"){
            return "https://sec.paymentexpress.com/pxaccess/pxpay.aspx";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.