Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing class variables
    primarykey
    data
    text
    <p>It's the first time I use OOP with PHP 5.. so this is my problem..</p> <p>I have a file <code>disp.php</code> that contains a class named class disp (model in MVC)</p> <pre><code>&lt;?php class disp{ public $n_pages; public $current_page; private $cmd2; /***************SQL command generator*******************/ private function getCmd2($cmd1,$id,$first_entry,$perpage,$tri){ $cmd2=str_replace('COUNT(*)','*',$cmd1); $cmd2=$cmd2.' ORDER BY '.$id.' '.$tri.' LIMIT '.$first_entry.','.$perpage; return $cmd2; } /********************Items display******************/ function dispItems($cmd1,$id,$perpage,$tri){ require('global/connection.inc.php'); try{ foreach($pdo-&gt;query($cmd1)as $r){ $n_pages=ceil($r[0]/$perpage); if (isset ($_GET['pg'])){ $current_page=intval($_GET['pg']); if ($current_page&gt;$n_pages){ $current_page=$n_pages; } if ($current_page&lt;=0){ $current_page=1; } } else{ $current_page=1; $_GET['pg']=1; } } $i=1; $first_entry=($current_page-1)*$perpage; $objet=new disp(); $cmd2=$objet-&gt;getCmd2($cmd1,$id,$first_entry,$perpage,$tri); $data=array(); $i=0; foreach($pdo-&gt;query($cmd2) as $r){ $data[$i]=$r; $i++; } return $data; }catch(PDOException $e){} } } </code></pre> <p>this is the file <code>news.php</code> (controller in MVC):</p> <pre><code>require MODELS_DIR.'disp.php'; $objet=new disp(); $news=$objet-&gt;dispItems('SELECT COUNT(*) FROM tbl_nouveautes','ID_EVENT',10,'DESC'); $c_page=$objet-&gt;$current_page; $n_pages= $objet-&gt;$n_pages; require VIEWS_DIR.'disp-news.php'; </code></pre> <p>in this code, I created an object (objet) of the type disp... I want to use the variables declared in the function <code>dispItems</code>, (<code>$n_pages</code> and <code>$current_page</code>) in the view (<code>disp-news.php</code>)</p> <p>so I think that the class variables are the same variables in the function <code>dispItems()</code>... but when trying to access them from the controller ...using object. it shows me a error :</p> <p>See:</p> <pre><code>Notice: Undefined variable: n_pages in C:\Program Files\EasyPHP-5.3.6.1\www\example\admin\global\news.php on line 14 Fatal error: Cannot access empty property in C:\Program Files\EasyPHP-5.3.6.1\www\example\admin\global\news.php on line 14 </code></pre> <p>Inspite of <code>$n_pages</code> and <code>$current_pages</code> being public in the class disp</p> <p>thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload