Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing class variables

It's the first time I use OOP with PHP 5.. so this is my problem..

I have a file disp.php that contains a class named class disp (model in MVC)

<?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->query($cmd1)as $r){
       $n_pages=ceil($r[0]/$perpage);
           if (isset ($_GET['pg'])){
          $current_page=intval($_GET['pg']);
               if ($current_page>$n_pages){
                    $current_page=$n_pages;
               }
               if ($current_page<=0){
                   $current_page=1;
               }
           }
           else{
           $current_page=1;
           $_GET['pg']=1;
           }
    }


    $i=1;
    $first_entry=($current_page-1)*$perpage;

    $objet=new disp();
     $cmd2=$objet->getCmd2($cmd1,$id,$first_entry,$perpage,$tri);

    $data=array();
    $i=0;
    foreach($pdo->query($cmd2) as $r){
    $data[$i]=$r;
    $i++;
    }

  return $data;
  }catch(PDOException $e){}
}
}

this is the file news.php (controller in MVC):

require MODELS_DIR.'disp.php';
$objet=new disp();
$news=$objet->dispItems('SELECT COUNT(*) FROM tbl_nouveautes','ID_EVENT',10,'DESC');

$c_page=$objet->$current_page;
$n_pages= $objet->$n_pages;


require VIEWS_DIR.'disp-news.php';

in this code, I created an object (objet) of the type disp... I want to use the variables declared in the function dispItems, ($n_pages and $current_page) in the view (disp-news.php)

so I think that the class variables are the same variables in the function dispItems()... but when trying to access them from the controller ...using object. it shows me a error :

See:

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

Inspite of $n_pages and $current_pages being public in the class disp

thank you in advance

like image 887
SmootQ Avatar asked Aug 21 '11 16:08

SmootQ


1 Answers

c_page=$objet->$current_page;
n_pages= $objet->$n_pages;

should be

$c_page=$objet->current_page;
$n_pages= $objet->n_pages;
like image 61
Dogbert Avatar answered Sep 25 '22 00:09

Dogbert