I'm relatively new to OO PHP, and frankly PHP in general. I have a class that I assign the array values in the constructor. However, when I access the array later, it is telling me that the array is null. Any ideas how this is going out of scope?
class SentenceContentContainer {
public $strSentence; //theSentence entered
public $arrayOfWords = []; //words in the sentence
private $num_words_in_sentence;
function __construct($strSentence)
{
$this->strSentence = $strSentence;
$arrayOfWords = explode(" ", $strSentence); //get the array of words in the string
$num_words_in_sentence = count($arrayOfWords); //count elements in the sentence
}
function sortHighestLetterRepeaterOfSentence()
{
usort($arrayOfWords, "compare"); //says parameter 1 is null
}
...
}
This is accessed from:
<html>
<head><title>PHP Code</title></head>
<body>
<?php
include "classes.php";
//process the data input
$post_string = implode("",$_POST); //change post array to string
// instantiate 1 of 2 objects
$sentenceCC = new SentenceContentContainer($post_string);
call_user_method("sortHighestLetterRepeaterOfSentence",$sentenceCC);
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
When I tried adding this->arrayOfWords in the Sentence contructor, it said it was a syntax issue.
I wonder if the issue is that somehow it's running the call_user_method even though I haven't hit submit yet in the form, after entering the sentence? I wouldn't think it would have gotten there yet?
Added: When I invoke the script in the browser, before I hit submit in the form, is when I see the warning message.
Added Also: Maybe I need to check that $arrayOfWords is not null or something in sortHighestLetterRepeaterOfSentence? I tried adding a check for null, but It's saying Undefined variable arrayOfWords where I test it for != null. I was also considering isset, but it's unclear that this would fix it.