CodeIgniter Forums
Call to a member function on a non-object Issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Call to a member function on a non-object Issue (/showthread.php?tid=65890)



Call to a member function on a non-object Issue - codeigniterd - 08-06-2016

I want to put all my email  send code from a library.

i have craeted a controller function resetPassword
controller is forgot_password.php
public function resetPassword()
    {
        //Reset Password Send Mail Code Here
        $this->load->helper('form');
        $this->load->library('form_validation','security');

        $this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email');
        if ($this->form_validation->run() == FALSE)
        {
            $message = array('type' => 'alert', 'text' => 'Please submit valid email !', 'title' => 'ERROR');
        }
        else
        {
            $this->load->model('login_model');
            $email = $this->input->post('email');
            $email = $this->security->xss_clean($email);
            $user_exists = $this->login_model->where('user_username', $email)->get();
            if($user_exists)
     {
                    $params = array(
                        'user_username'=>'hjsjajdjaa',
                        'link'=> 'link'
                    );
                    $this->load->library('backendEmailLibrary');
                    $responseFromMail = $this->backendEmailLibrary->sendResetPasswordMail($params);
                    if($responseFromMail)
                      echo 'success';
                }
            }
            else
            {
                   echo 'faliure';
            }
        }
       echo 'exit';
    }


my library
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class BackendEmailLibrary {

    protected $CI;
    // Constructor
    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('login_model');
        if (ENVIRONMENT == 'production')
        {
            $config = Array(       
                'protocol' => 'sendmail',
                'smtp_host' => 'your domain SMTP host',
                'smtp_port' => 25,
                'smtp_user' => 'SMTP Username',
                'smtp_pass' => 'SMTP Password',
                'smtp_timeout' => '4',
                'mailtype'  => 'html',
                'charset'   => 'iso-8859-1'
            );
        }
        else
            $config = Array();
    }

    public function sendResetPasswordMail($params)
    {
        $user_username = $params['user_username'];
        $token_link = $params['token_link'];
        $subject = 'password reset mail';
        echo '<pre>'; print_r($config); die;
        $this->CI->load->library('email', $config);
        if(!empty($config))
        {
            //Reset Mail starts
            $this->CI->email->set_newline("\r\n");
            $this->CI->email->from('[email protected]', 'xxxx');
            $user_exists = $this->CI->login_model->where('user_username', $user_username)->get();
            $data = array(
                'pssrestmailName'=> $user_exists->user_fname,
                'pssrestmailToken'=> $link
            );
            $this->CI->email->to($user_exists->user_username);  // replace it with receiver mail id
            $this->CI->email->subject($subject); // replace it with relevant subject

            $body = $this->CI->load->view('emails/resetpassword.php',$data,TRUE);
            $this->CI->email->message($body);  
            $flag = $this->CI->email->send();
            return $flag;
        }
        else
            return false;
    }
}

=====================================================
I dont knw where from the error is coming from ? Please help


Severity: Notice
Message: Undefined property: Forgot_password::$backendEmailLibrary
Filename: controllers/forgot_password.php

Call to a member function sendResetPasswordMail() on a non-object in forgot_password.php on line Bolded area.


RE: Call to a member function on a non-object Issue - InsiteFX - 08-06-2016

Available Visibility in PHP Classes

There are 3 types of visibility available in php for controlling your property or method.

1) Public: Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child(will discuss in next chapter) class also.

2) Private: Method or property with private visibility can only be accessible inside the class. You can not access private method or variable from outside of your class.

3) Protected: Method or variable with protected visibility can only be access in the derived class. Or in other word in child class. Protected will be used in the process of inheritance.


RE: Call to a member function on a non-object Issue - codeigniterd - 08-08-2016

(08-06-2016, 03:48 AM)InsiteFX Wrote: Available Visibility in PHP Classes

There are 3 types of visibility available in php for controlling your property or method.

1) Public: Public method or variable can be accessible from anywhere. I mean from inside the class, out side the class and in child(will discuss in next chapter) class also.

2) Private: Method or property with private visibility can only be accessible inside the class. You can not access private method or variable from outside of your class.

3) Protected: Method or variable with protected visibility can only be access in the derived class. Or in other word in child class. Protected will be used in the process of inheritance.

Fixed ! Thanks For your help Smile