0

I am trying to learn oop and I'm trying to pass a value from one one function to another inside a class but for some reason it gives me an error of Notice: Trying to get property of non-object, any ideas?

class test{
    function test($value){
    global  $db;
    $stmt = $db->prepare("SELECT * FROM some_table where some_column = ?");
    $stmt->bind_param('s', $value);
    $stmt->execute();
    $res = $stmt->get_result();
    $fetch = $res->fetch_object();
    $this->test = $fetch->some_row;//this is the error line 
}
      function do_something(){
        $name = $this->test;
         return $name;
      }
      }
$p = new test();
$p->test('test');
echo $p->do_something();
5
  • 1
    It also points you to the exact line where it happens. Since you know a line number and variable - take var_dump() and check the actual variable value. Commented Sep 27, 2014 at 3:27
  • @zerkms when I do the var_dump it returns NULL. Commented Sep 27, 2014 at 3:48
  • so - that's why you get an error. You are trying to invoke a method of NULL, which is nonsense. Commented Sep 27, 2014 at 3:49
  • @zerkms, okay but it turns out that the $value is the prob, if I give it some other value for the bind_param it works. Do you have any idea why the value for function test() not passing? Commented Sep 27, 2014 at 3:59
  • lol nope, it is not working. the value isn't being passed. Commented Sep 27, 2014 at 4:04

2 Answers 2

1

Try out following code:

<?php

    class test {

        /**
         * @var $test
         **/
        public $test;

        /**
         * Constructor of current class
         **/
        function __construct($value = "") {

            /**
             * Global variable $db must be defined before use at here
             **/
            global  $db;

            $stmt = $db->prepare("SELECT * FROM some_table where some_column = ?");
            $stmt->bind_param('s', $value);
            $stmt->execute();
            $res = $stmt->get_result();
            $fetch = $res->fetch_object();

            $this->test = $fetch->some_row; // Set return value to public member of class
        }

        /**
         * Process and get return value
         **/
        function do_something() {

            $name = $this->test;
            return $name;
        }
    }


    $p = new test('test');
    // $p->test('test'); // You don't need to call this function, because this is the constructor of class 
    echo $p->do_something();
Sign up to request clarification or add additional context in comments.

1 Comment

Relying on the constructor method being the same name as the class is deprecated - you really should call it __construct.
0
class Test{

    public function test($value){
        global  $db;
        $stmt = $db->prepare("SELECT * FROM some_table where some_column = ?");
        $stmt->bind_param('s', $value);
        $stmt->execute();
        $res = $stmt->get_result();
        $fetch = $res->fetch_object();
        $var_set = $fetch->some_row;//this is the error line 
        return $var;
    } // end the funtion 

    function do_something($value){
        $name = $this->test($value); // you have to pass an value here 
        return $name;
    }
}

$p = new Test;
$return_value = $p->do_something($value);
print_r($return_value);

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.