Php 如果语句和数组不工作,则更智能

Php 如果语句和数组不工作,则更智能,php,if-statement,Php,If Statement,我想创建一种更智能的方法来创建if语句。我正在编写一个函数来实现这一点: if ( ! function_exists( 'get_meta' ) ) { function get_meta($i) { $fname_name = array( 'copyright_text', 'about_name', 'archive_name',

我想创建一种更智能的方法来创建if语句。我正在编写一个函数来实现这一点:

if ( ! function_exists( 'get_meta' ) ) {
    function get_meta($i) {
        $fname_name = array(
                    'copyright_text',
                    'about_name',
                    'archive_name',
                    'contact_name',
                    'lenguage_name',
                    'cc_name',
                    'about_link',
                    'archive_link',
                    'contact_link',
                    'lenguage_link',
                    'cc_link',
                    'about_editors_name',
                    'about_responsibility_name',
                    'about_joinus_name',
                    'about_editors_link',
                    'about_responsibility_link',
                    'about_joinus_link'
                    );
        foreach( $fname_name as $fname )
            include_once( get_option($fname));

        if ( $i ) return $fname_name[$i];
    }
}
但调用此函数时,它会返回以下错误:

警告:include_once()[function.include]:在第398行的local\wp content\themes\net\0.6\functions.php中打开要包含的内容(include_path=';php\PEAR')失败

基本上,只需要添加
get_选项(“”)到每个数组,以返回,例如:

get_option('copyright_text');
或者更具体地说,返回:

get_option('copyright_text', '');
固定的:

好的,我只是自己解决这个问题,但是我非常感谢这里的任何建议

我使用了一个更简单的解决方案,而不是使用
foreach
include_once

if ($i) return get_option($fname_name[$i], '');
else if ($i == 0) return get_option($fname_name[0], '');
用这个

 foreach( $fname_name as $fname ){
        include_once( get_option($fname));
       if ( $i ) return $fname;
 }

看起来您正在创建一个用于编写访问器方法的快捷方式。看看PHP的神奇方法会对你自己有好处。特别注意uuuu get、uuuu set和uuu call

在这种情况下,您所做的与以下内容类似:

class myClass {

    // This could be associated with whatever other data you are interested in
    private $_meta = array(
                'copyright_text',
                'about_name',
                'archive_name',
                'contact_name',
                'lenguage_name',
                'cc_name',
                'about_link',
                'archive_link',
                'contact_link',
                'lenguage_link',
                'cc_link',
                'about_editors_name',
                'about_responsibility_name',
                'about_joinus_name',
                'about_editors_link',
                'about_responsibility_link',
                'about_joinus_link'
                );

    public function __get($name) {
        if (in_array($name, $this->_meta)) {
            return $this->_meta[$name];
        }
    }
}

有一个问题是显而易见的:返回
$fname[$i]
,而此时不存在名为
$fname
的变量。那么
get\u option()
是如何定义的?或者一个更好的问题,你想在这里做什么?这段代码有一股难闻的味道。我会将echo json_encode(get_option($fname))添加到foreach语句中,看看这个get_选项返回的是什么。我想把简单的东西变成复杂的东西不是更聪明的方法吗?我这样做是因为我不想写上百条if语句。