duan1443 2016-05-05 18:36
浏览 136
已采纳

OO PHP数组值丢失或丢失

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.

  • 写回答

2条回答 默认 最新

  • dongzhao5970 2016-05-05 18:39
    关注

    $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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题