doumicheng6732 2014-11-12 17:18
浏览 35

访问类的属性

I'm coding a small piece for a larger project that will read a CSV file and store the information in a class. I'm storing each instance of the class in an array.

So far, all I'm trying to do is read each line of the CSV, create a new class instance with that info, and then display the info using a class function I created.

I have two questions:

first, when building the constructor, I originally tried using $this->$property but I got undefined variable errors for each property when I did that. I took out $this and it seemed to work alright. I'm wondering why $this didn't work there?

And now the error that I'm having is an undefined variable error for each time I try to access a variable to print it out in the displayInfo() function of my class. I don't understand how the variable is undefined, they are properties of the class and they were initialized using the constructor.

Here's my code:

<?php
class Course {
public $crn;
public $title;
public $instructor;
public $section;
public $location;
public $time;
public $days;

function __construct($arg_crn, $arg_title, $arg_instructor, $arg_section, $arg_location, $arg_time, $arg_days) {
    $crn = $arg_crn;
    $title = $arg_title;
    $instructor = $arg_instructor;
    $section = $arg_section;
    $location = $arg_location;
    $time = $arg_time;
    $days = $arg_days;
}

public function displayInfo() {
    echo ("
");
    echo $crn;
    echo (", ");
    echo $title;
    echo (", ");
    echo $instructor;
    echo (", ");
    echo $section;
    echo (", ");
    echo $location;
    echo (", ");
    echo $time;
    echo (", ");
    echo $days;
    echo ("<br/>");
}
}
    ?>


    <?php
            $fileName = "testCSV.txt";
            $fp = fopen($fileName, "r");
            $index = 0;
            // get the next line of the CSV: this contains the headers
            $fields = fgetcsv($fp);
            // get the next line of the CSV, which contains the first course information
            // $fields is an array holding each field of the line (where a field is separated by commas)
            $fields = fgetcsv($fp);
            while($fields) {
            // if at the end of the file, fgetcsv returns false and the while loop will not execute
                    // the fields in the file are saved into the following indices in fields:
                    // 0: CRN
                    // 1: Title
                    // 2: Instructor
                    // 3: Section
                    // 4: Location
                    // 5: Time
                    // 6: Days

                    // add a new course to the array of courses using the information from fields
                    $Courses[$index] = new Course($fields[0], $fields[1], $fields[2], $fields[3], $fields[4], $fields[5], $fields[6]);
                    $Courses[$index]->displayInfo();
                    // get the next line and increment index
                    $fields = fgetcsv($fp);
                    $index++;
            }
    ?>
  • 写回答

1条回答 默认 最新

  • dongzhu3548 2014-11-12 17:34
    关注

    As Mark Baker mentioned that instance variable can be accesses using $this->crn, follow the same & read some tutorials on OOPS in PHP as you are too new to this to ask anything here.

    You tried accessing properties using $this->$property, which should be $this->property.

    Some tutorials:

    http://php.net/manual/en/language.oop5.php

    http://www.tutorialspoint.com/php/php_object_oriented.htm

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么