【问题标题】:not sure why I'm getting "Attempt to modify property of non-object in"不知道为什么我得到“尝试修改非对象的属性”
【发布时间】:2014-06-03 17:58:34
【问题描述】:

我正在编写一个将信息输入容器类的函数。我不断收到错误Attempt to modify property of non-object in。涉及的课程很多,如果有人愿意看一下,我可以上传整个项目。所有单独的类都已经过功能测试并按预期执行,它只是通过给出错误的这个函数分配新类中的值。我在这里附加了容器类和驻留在另一个类中的函数。非常感谢您的帮助!

class masterContainer{
    public $mc = array('file','line' => array('line' => array('string','element'=>array())));//this array stores all the contents needed for a file
    public function __construct($file){
        $this->file = $file;
        $this->mc['file'] = $this->file;
        $mc['line'] = array();
        $mc['line'][$l]['string'] = $line[$l];
        $mc['line'][$l]['element'] = array();
        $mc['line'][$l]['element'] = $element[$e];
    }//end __construct()
}//end masterContainer{}

这是函数

<?php
require_once('/Library/WebServer/Documents/gasDev/super_src/controller/fileController.php');
require_once('/Library/WebServer/Documents/gasDev/super_src/container/masterContainer.php');
require_once('/Library/WebServer/Documents/gasDev/super_src/controller/lineController.php');
require_once('/Library/WebServer/Documents/gasDev/super_src/controller/elementController.php');

class masterControl{

public $fc;//fileHandling
public $container;////masterContainer
public $lc;//lineController
public $ec;//elementController


public function __construct($path){
    $this->buildContainer($path);

}//end __construct()

public function buildContainer($path)
{
    $this->fc = new fileController($path);//create files
    foreach($this->fc->getFiles() as $f){$this->container[] = new masterContainer($f);};//create containers, assign file to memory
//      foreach($this->container as $c){echo $c->file."<br>";};//lists all files in container
    $i=0;//a counter
    foreach($this->container as $c){$this->lc[] = new lineController($c->file);$i++;};//create new lines
//assign lines to memory
    $i=0;//a counter
    foreach($this->lc as $line){
        $j=0;//a counter
        //echo $i."<br>";
        foreach($line->getLines() as $l){
            $this->container[$i]->line[$j] = $l;
            //echo $l."<br>";
            $j++;
        };//assign line to memory
        //echo "<br>";
        $i++;
    };


//make elements
$i=0;
foreach($this->container as $c){
    //echo count($c->line)."<br>";
    //echo "=-".$i."-=<br>";
    $j=0;
    foreach($c->line as $l){
        //echo $i." - ".$l."<br>";
        $this->ec[] = new elementController("*",$l);

        foreach($this->ec as $elements){
            $k=0;
            foreach($elements->getElements() as $e){
                //echo $e."<br>";
                $this->container[$i]->line[$j]->element[$k] = $e;
                //echo $k."<br>";
                $k++;
            };
        };
        //echo "__".$j."__<br>";
        $j++;
    };
    $i++;
};
//      echo "<br><br>";
//      echo "file: ".$this->container[0]->file."<br><br>";
//      echo "line: ".$this->container[0]->line[0]."<br>";
//      echo "element: ".$this->container[0]->line[0]->element[0]."<br>";
}//end buildContainer()

public function echoArray($array){foreach ($array as $a){echo $a."<br>";};}
public function __toString(){}

}//end mastController{}
$mc = new masterControl('/Library/WebServer/Documents/gasDev/test_files/850_855_csv_tests/850');
?>

【问题讨论】:

  • 错误发生在哪一行?
  • $this->container[$i]->line[$j]->element[$k] = $e;//将元素分配给这一行的内存,从底部算起9。我在它之前添加了一条评论
  • 根据您的其他 cmets,它看起来像 line[$j] 没有被实例化(或者它被正确分配并且没有被正确分配)。如果你运行var_dump($this-&gt;container[$i]-&gt;line[$j]),你会得到什么?如果它不是您期望的对象,那将是从哪里开始向后工作。
  • 我添加了函数所在的整个类
  • @Crackertatstic - 我从var_dump($this-&gt;container[$i]-&gt;line[$j]) 中得到了我的期望我得到错误

标签: php arrays containers


【解决方案1】:
$this->container

未定义,应初始化为数组。

或者:

$this->container[$i]->line[$j]->element[$k]

行或元素未初始化。

调试这样的代码真的很难......

【讨论】:

  • 我可以分配给 $this->container[]->file 以及 $this->container[]->line[] 但是当我尝试分配给 $this-> container[]->line[]->element[] 我得到错误
  • 对不起,如果有帮助,我添加了整个课程。感谢您的反馈。我很确定$this-&gt;container[$i]-&gt;line[$j]-&gt;element[$k] 没有被实例化我只是不确定我需要怎么做
猜你喜欢
  • 2017-09-17
  • 2017-12-12
  • 2019-11-12
  • 1970-01-01
  • 2019-05-19
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多