数组中的调用函数(PHP) [英] Call function in array (PHP)

查看:79
本文介绍了数组中的调用函数(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建数组的过程中,我试图调用一个函数 checkIfNodeExists()。 PHP执行此功能并给我预期的结果,但他仍然给我一个通知,并且我不希望代码中出现任何错误。

In the building process of an array I'm trying to call a function, checkIfNodeExists(). PHP executes this function and gives me the expected result but he still gives me a "Notice", and I don't want any kind of errors in my code.

function checkIfNodeExists($input) {
   if (isset($input)) {
      return (string) $input;
   } else {
      return 'null';
   }
}

$array['sections'][] = array (
   'ident'       =>  $section->attributes()->ident,
   'title'       =>  $section->attributes()->title,
   'objective'   =>  checkIfNodeExists($section->objectives->material->mattext)
);

注意:尝试获取/ var / www / OLAT_Connection / QTI-XML中非对象的属性第xx行上的-Parser.php

Notice: Trying to get property of non-object in /var/www/OLAT_Connection/QTI-XML-Parser.php on line xx

现在,如果我检查 objective是否在数组之外,PHP不会给我通知。但这会导致更多的代码行,因为我必须使用额外的variabele和更多的IF结构,以此类推...

Now, if I check if "objective" exists OUTSIDE the array, PHP doesn't give me a notice. But this results in more lines of code because I have to work with an extra variabele and more IF-structures and so on...

是否有任何其他可能性而无需添加

Is there any other possibility without adding too much lines of extra code?

推荐答案

似乎 $ section-> objectives-> material-> mattext 定义不正确。我会开始寻找那里的对象的初始化是否有错误。

It seems that $section->objectives->material->mattext is not properly defined. I would start looking there to see if you have errors in the initialisation of the object.

如果您发布更多的代码会更好,这样我们就可以看到确切的内容了。

It would be better if you posted more code up so we can see what exactly is going on in this script.

该解决方案可能需要更多代码(尽管在这种情况下不太可能),但这绝不是一件坏事。更少的代码不一定更好或更有效!显然,不用说(在大多数情况下)更少的代码行可以更快地执行,但这并不能使其更加安全或高效

更新

您可以简单地执行此操作,而不用调用另一个函数:

You may be able to simply do this instead of calling another function:

'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : null

我有没有测试过,并且不记得您是否可以将条件语句内联,因此无法确定它是否可以工作,但是如果这样做可以提高效率,并且代码更少!

I have not tested this and cannot remember whether you can place conditional statements inline, so cannot be sure whether it will work but if it does then this will be more efficient, and it is less code!

这篇关于数组中的调用函数(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆