1

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.

1
  • 2
    Why not $sentenceCC->sortHighestLetterRepeaterOfSentence()? And yes, $this is reqired. Commented May 5, 2016 at 18:38

2 Answers 2

3

$arrayOfWords is a variable that only exists inside the __construct function.

$this->arrayOfWords is a private class variable that exists in any method of the class and has a different value per-instance.

P.S. Why are you using call_user_method? This function is deprecated (and I think removed in PHP 7). Just a quick note, if you saw that in a tutorial, you should consider a new tutorial as that one's gonna be outdated.

You can just do:

$sentenceCC->sortHighestLetterRepeaterOfSentence()

If you must, you can use call_user_func instead:

call_user_func([$sentenceCC, 'sortHighestLetterRepeaterOfSentence']);
Sign up to request clarification or add additional context in comments.

2 Comments

Are you saying that if I want to add to arrayOfWords in the constructor and use it in other functions later that I need to assign it using $this->arrayOfWords? That's not clear to me.
@Michele: Yes. You need to use $this->arrayOfWords in every method of the class, including the constructor. In the constructor, you can set its value.
1

Yes this code will execute even if form is not submitted.

I think your should check $_POST variable and allow to run your code only

if (count( $_POST ))

Comments

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.