2013-05-14 42 views
-1

更新:我得到这个PHP的错误:试图让该行非对象的属性: 如果($卡 - >名== $ fileRef){PHP构造函数单PARAM

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

构造函数接收一个字符串,即与相应的'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 

的错误:我不能得到$ fileRef变量与JSON文件阵列中的任何东西搭配起来,所以它总是进入“否则,如果”默认。 JSON文件看起来是这样的:

{"Cards":[{"title":"Something", "name":"file.php", "dir":"somefile/dir/here/" }, {"title":"Different than something", "name":"xfiles.php", "dir":"somefile/dir/there/" ...etc

+0

什么是输出行'echo:$ val ['name']'? – 2013-05-14 17:28:48

+0

谢谢这已被回答。 – 2013-05-14 18:14:15

回答

0

您可能要遍历整个数组返回或设置一个通用的案前。这里是如何做到这一点:

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. 
+0

Explosion Pills:调试回声语句输出start.php(我想要的)它在foreach中没有匹配。 Sabrina:你是绝对正确的。我犯了一个人为错误,你发现了这一点,就像我刚发布这个问题一样。我会做出一些改变,并保持张贴* – 2013-05-14 17:41:43

+0

你的start.php输出是在foreach之后完成的,不管发生了什么。否则,你的foreach只会跨越数组的第一个元素。 – 2013-05-14 17:44:14

+0

问题出在if else和我想要输出的语法上。 我也必须访问解码的json,因为这样的 $ this-> cardArray ['Cards']作为$ card => $ val ... $ this-> name = $ val ['name']; – 2013-05-14 18:15:24