PHP构造函数单参数



更新:我得到这个php错误:试图获得这行非对象的属性:if ($card->name == $fileRef){

我在php中使用__construct(x='') { definition }构建一个对象并调用函数$var = new Object('string');

构造函数接收一个字符串,例如'file',它与对应的'file.php'相关。在json文件中有一个目录,其中包含该类的所有可用file.php,其中包含目录信息。

我正在捕捉一个错误,也许与我的语法?…我还有两天时间,想试试吗?

public function __construct($ctitle = '')
{
    $fileRef = $ctitle.'php';
    //Get the json card directory
    $this->cardDirLocation = 'thisismyserver.com/CardDir.json';
    $this->cardDir = file_get_contents($this->cardDirLocation);
    $this->cardArray = json_decode($this->cardDir, true);

    //Find the card listing from CardDir.json based on form response input and construct a Card class instance or use the main page (default)
    foreach ($this->cardArray['Cards'] as $key => $val) { //search through each card IS THIS MY ERR??
        if ($card->name == $fileRef){ //if the name matches a name in the cardDir.json file
            //Fill Values
        }
                    if ($this->title == ''){ //or if there is no title
            $this->title = 'Get Started at The Home Page'; //refer to default values -> the home page
            $this->dir = 'cards/start/';
            $this->name = 'start.php'; ...etc 

Err:我无法获得$fileRef变量与json文件数组中的任何内容匹配,因此它总是进入'else if'默认值。json文件看起来像这样:

{"卡片":[{"标题":"东西","名称":"file.php","dir":"这里somefile/dir//"},{"title":"不同于某物","名称":"xfiles.php","dir":"somefile dir//"等

您可能希望在返回或设置一个通用大小写之前循环遍历整个数组。方法如下:

foreach ($this->cardArray['Cards'] as $card) {
    if ($card->name == $fileRef) {
    // Your success actions here
     return true;
    }
}
 // Your failure actions here. This will only be reached if the foreach loop found nothing.

相关内容

  • 没有找到相关文章

最新更新