问题描述
这是我第一次在PHP 5中使用OOP.所以这是我的问题.
It's the first time I use OOP with PHP 5.. so this is my problem..
我有一个文件disp.php
,其中包含一个名为class disp的类(MVC中的模型)
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){}
}
}
这是文件news.php
(MVC中的控制器):
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';
在此代码中,我创建了一个类型为disp的对象(objet).我想在视图(dispItems,($n_pages
和$current_page
)中声明的变量. >)
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
)
所以我认为类变量与函数dispItems()
中的变量相同,但是当尝试使用对象从控制器中访问它们时.它显示了一个错误:
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 :
请参阅:
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
尽管$n_pages
和$current_pages
在disp类中是公开的
Inspite of $n_pages
and $current_pages
being public in the class disp
先谢谢您
推荐答案
c_page=$objet->$current_page;
n_pages= $objet->$n_pages;
应该是
$c_page=$objet->current_page;
$n_pages= $objet->n_pages;
这篇关于访问类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!