1

I'm still new to PHP and I am having a lot of trouble. I'm used to languages like C,C++, and Java, and this one is kinda confusing me. Basically my problem is that I have the following code:

class File_Reader 
{

    protected $text;

    public function Scan_File (){}

    public function Skip_Whitespace(&$current_pos)
    {
        //skip past whitespace at the start
        while (($current_pos < strlen($text) && ($text[$current_pos] == ' ')))
            $current_pos++;
    }

    public function __construct(&$file_text)
    {
        $text = $file_text;
    }
}

class Times_File_Reader extends File_Reader
 {
     Public Function Scan_File()
     {
         $errors = array();
         $times = array();
         $current_time;
         $cursor = 0;
         $line = 0;
         while ($cursor < strlen($text))
         {

             Skip_Whitespace($cursor);
             //....lots more code here...
             return $times;
         }
     }
 }

but when I try to run it, it tells me that $time and Skip_Whitespace are both undefined. I don't understand, they should have been inherited. I tried putting an echo command in the File_Reader constructor and it does enter the constructor when I create my Times_File_Reader.

Oh, and for completeness, here is where I declare my Times_File_Reader:

   include 'File_Readers.php';

  $text = file_get_contents("01_CT.txt");
  $reader = new Times_File_Reader($text);
  $array = $reader->Scan_File();

I've been searching for an answer for hours to no avail, and deadline is approaching quickly. Any help would be appreciated. Thank you!

2 Answers 2

2

I believe that you need to note that you are using the class function by using

$this->Skip_Whitespace($cursor);

Sign up to request clarification or add additional context in comments.

2 Comments

It goes deeper than just $this->Skip_Whitespace($cursor); you also need to use $this->text as the next answer suggests.
@PhillSparks you are correct. I was confused by his reference to $time, when I think he meant $text.
1

You need to set the property that you are passing into your constructor as a property of the class (the variables within methods are scoped in the same way as Java).

You do this using $this->property

// File_Reader
public function __construct(&$file_text)
{
    $this->text = $file_text;
}

// Times_File_Reader
public function Scan_File()
{
    $errors = array();
    $times = array();
    $current_time;
    $cursor = 0;
    $line = 0;
    while ($cursor < strlen($this->text))
    {
        $this->Skip_Whitespace($cursor);
        //....lots more code here...
        return $times;
    }
}

Edit - as an aside you seem to be using some wacky underscorecase/Titlecase hybrid. Best practice in PHP is to use lowerCamelCase for methods and CamelCase for class names.

class FileReader
class TimesFileReader
public function scanFile()

Also - you are passing your variables by reference (&$var) - you probably don't have to do this (the only valid use case I can think of for this is in certain situations using closures/anonymous functions). I'll admit the docs on this are not very clear. http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html

public function __construct($file_text)
{
    $this->text = $file_text;
}

3 Comments

Ok, this is helping. Thank you! However, I still cannot use Skip_Whitespace($cursor); The error it gives me is: Fatal error: Call to undefined function Skip_Whitespace() in C:\xampp\htdocs\Software-Engineering\File_Readers.php on line 58 Any clue why that part is not working?
Again - you need to use $this->method(). Updated the code accordingly.
Ok. Thank you. Also, as far as the "wacky underscorecase/Titlecase hybrid" goes, that was not my choice. Team captain does not like camel case for some reason and refused to let it be part of our coding standards. :\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.