susanBuck / e15-spring21

1 stars 0 forks source link

passing multiple variables in constructor class not passing properly #18

Closed caronee closed 3 years ago

caronee commented 3 years ago

Repository URL https://github.com/caronee/e15/tree/main/practice

Production URL http://e15practice.caroli.me/

Describe the problem

I was rewriting my p1 because I was trying to push two variables using constructor/ classes instead of just one as in the professor's example videos.

I am getting an error when I run my public functions. I var_dumped in the constructor and in the isPalindrome function so you can see the output.

As you can see, the constructor works and I am getting values while inside it, but once I'm in the isPalindrome function (and any other function), I get undefined variables and properties because I'm not calling the variables correctly. I've tried to stackexchange the error and they seemed to say it was a 'this->' pointing error but I'm not sure where the pointing error is. I've attached a picture of what it looks like locally because it shows more errors than the production url.

image

I'm not sure if the error is in my process.php file and I have not written the _session part correctly or if the error is in how I call the variables in my actual functions.

image

To Reproduce Steps to reproduce the behavior:

  1. fill in any value for the form
  2. See error say it's null after the var_dump from the constructor
susanBuck commented 3 years ago

Hi @caronee -

In practice/StringProcessor.php on line 22 this line:

var_dump($this->$answer);

...should be:

var_dump($this->answer);

Same issue on lines 24 and 53.

A $ is not necessary in front of property names when accessing them via $this. You have it correct in the construct method when working with $this->answer.

The last error Call to undefined function countVowels() is because this line:

$vowelCount += countVowels($letter);

Should be:

$vowelCount += $this->countVowels($letter);

countVowels is a method within the class, so we want to access it via $this.

Let me know if you have any additional questions or issues after making the above fixes.

caronee commented 3 years ago

Thanks professor! I got it to work. I'm a little embarrassed it was something so small haha. I need to watch out for methods being called within the class too.

susanBuck commented 3 years ago

@caronee - Excellent, glad it's working! We've all had the experience of missing little characters here and there. Sometimes it just takes a set of "fresh eyes" to be able to spot the problems, so never hesitate to post questions like these. : )