php 访问类变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7139508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:08:09  来源:igfitidea点击:

Accessing class variables

phpoopclassvariables

提问by SmootQ

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

这是我第一次在 PHP 5 中使用 OOP .. 所以这是我的问题..

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

我有一个文件disp.php,其中包含一个名为 class disp 的类(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):

这是文件news.php(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_pagesand $current_page) in the view (disp-news.php)

在这段代码中,我创建了一个类型为 disp 的对象(objet)...我想在视图()中使用函数中声明的变量dispItems,($n_pages和)$current_pagedisp-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 :

所以我认为类变量在函数中是相同的变量dispItems()......但是当试图从控制器访问它们时......使用对象。它向我显示了一个错误:

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_pagesand $current_pagesbeing public in the class disp

的Inspite$n_pages$current_pages公职类中的DISP

thank you in advance

先感谢您

回答by Dogbert

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

should be

应该

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

回答by a1ex07

In the body of non-static class methods you need to access class members like $this->current_page, not $current_page

在非静态类方法的主体中,您需要访问类成员,例如$this->current_page,而不是$current_page