Trying to get property of non-object in OOP php
<p>I just want to back my class properties so that I can use them. In my project I want to fetch all result of mysql table data and return it. Well it works well, but alter returning data the error message has come which is given below:</p>

<pre><code>   Trying to get property of non-object in E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
</code></pre>

<p>My code :: </p>

<p>nonClass.php</p>

<pre><code> &lt;?php
      $doc = new Dortor();  // object  

      $doc-&gt;result("SELECT * FROM doctor");   // function call

      foreach( $doc as $doctor ) {
         echo $doctor-&gt;doc_name;     // error msg: Trying to get property of non-object in
                                   //E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
      }
  ?&gt;
</code></pre>

<p>dortor.php</p>

<pre><code>  &lt;?php
     class Dortor extends DatabaseObject {
       public $table_name = "doctor";
       public $id;
       public $doc_name;
       public $doc_pass;
       public $doc_img;     // image directory

       public function result($sql="") { 
         $result = $database-&gt;query($sql);  // run query from mysql database

         $result_array = array();   
         while( $row = $database-&gt;fetch_array($result)  ) {  // this while loop return actual result
             $result_array[] = $this-&gt;get_table_value($row);
         }
         return $result_array;
        }
     }
  ?&gt;
</code></pre>

<p>databaseObject.php</p>

<pre><code>  &lt;?php
       class DatabaseObject {
           public function get_table_value($record) {
              $className = get_called_class();  // get the class name
              $object = new $className;       // object call

              $arrayValue = array();  
              $i=0;   // inital array position
              foreach( $object as $key =&gt; $value )  {
                  $arrayValue[$i] = $key;  // get the class's object attributes
                  $i++;
              } 

              $i = 1;
             foreach ($record as $key =&gt; $value) {   // fetch the database result
                 if(array_key_exists($key, $arrayValue) ) {  // check if database's column name is exist on 'arrayValue' array,
                    $object-&gt;$arrayValue[$i] = $value;  //  if exist then, store value in 'Doctor' class's object attributes
                    $i++;
                 }
              } 
             return $object;
          }
       }
  ?&gt;
</code></pre>
