简体   繁体   English

PHP对象上的空数组属性

[英]empty array attribute on php object

This is my class,which reads a csv and store the info in some way 这是我的课程,它读取csv并以某种方式存储信息

<?php
    class CSV{
        private $data;

        function __construct($filename){
            $this->data = $this->getDataFromFile($filename);
        }

        public function __get($property){

            if(property_exists($this,$property)){
                return $this->$property;
            }
        }



        private function getDataFromFile($filename){
            $new_data = array();
            $result = array();
            if (($handle = fopen($filename,"r")) !== FALSE) {
                while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
                    array_push($result, explode(";", $data[0]));;
                }
                fclose($handle);
            }


            $header = $result[0];

            $in_columns = array();
            for ($j = 0 ; $j < count($result[0]); $j++){
                $new = array();
                for ($i = 1 ; $i < count($result); $i++){
                    array_push($new, $result[$i][$j]);
                }
                array_push($in_columns, $new);
            }

            $idx = 0;
            foreach ($header as $title) {
                $new_data[$title] = $in_columns[$idx];
                $idx++; 
            }
            //var_dump($new_data);//the content of $new_data its correct
            $this->data = $new_data;
        }
    }

?>

but wen I try to use the class 但是我尝试使用该类

$csv = new CSV('./csv/file.csv');
var_dump($csv->__get('data'));

the last var_dump shows a NULL value ¿What is wrong on the assignation of the value?It looks correct for me ,where cold be the problem?? 最后一个var_dump显示一个NULL值?分配值有什么问题?对我来说似乎正确,哪里出了问题?

The problem is that your CSV file ends with a blank line ( \\n after the last line). 问题是您的CSV文件以空行结尾(最后一行之后是\\n )。

Thus you're working on an empty array, pushing an unitialized variable (null) into the data array. 因此,您正在处理一个空数组,将一个单位化变量(空)推入数据数组。

You're calling $this->getDataFromFile($filename) in constructor and assigning it's value to $this->data . 您正在构造函数中调用$this->getDataFromFile($filename)并将其值分配给$this->data However... implementation of getDataFromFile() didn't actually return any value, so it's assigning NULL to this property. 但是... getDataFromFile()实现实际上没有返回任何值,因此它为该属性分配了NULL

You need to change getDataFromFile() to return value OR get rid of variable assignment in constructor - $this->data is already set in your method. 您需要将getDataFromFile()更改为返回值, 或者摆脱构造函数中的变量赋值- $this->data已在您的方法中设置。

Regarding __get() - it's a magic method . 关于__get() -这是一种神奇的方法 It checks if specified property exists and if so - return it's value. 它检查指定的属性是否存在,如果存在,则返回其值。 You won't call it this way. 您不会这样称呼。 Use following code (after making $this->data public): 使用以下代码(将$this->data公开后):

var_dump($csv->data);

OR prepare accessor for this property which will return this value. 为此属性准备访问器,它将返回此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM