4

我有一个像这样的简单代码:

        class o99_custom_fields {
            /**
            * @var  string  $prefix  The prefix for storing custom fields in the postmeta table
            */
            var $prefix = 'o99_';
            /**
            * @var  array  $customFields  Defines the custom fields available
            */
            var $customFields = array(

                array(
                    "name"          => "some_name",
                    "title"         => "some Title",
                    "description"   => "Some Desctiption Text",
                    "type"          => "k_upload",
                    "scope"         =>  array( "post" ),
                    "capability"    => "edit_post"
                ),

                array(
                    "name"          => "some_name2",
                    "title"         => "some Title",
                    "description"   => "Some Desctiption Text",
                    "type"          => "k_upload",
                    "scope"         =>  array( "post" ),
                    "capability"    => "edit_post"
                ),

                array(
                    "name"          => "some_name3",
                    "title"         => "some Title",
                    "description"   => "",
                    "type"          => "k_textarea",
                    "scope"         =>  array( "post" ),
                    "capability"    => "edit_post"
                ),
            );
... more functions and more code ...
    } // End Class

一切似乎都很好,

当我尝试更改一些数组值并将它们放在括号内时,问题就开始了()

例如 :

array(
                "name"          => "some_name",
                "title"         => __("some Title","text_domain"),// ERROR OCCUR
                "description"   => "Some Desctiption Text",
                "type"          => "k_upload",
                "scope"         =>  array( "post" ),
                "capability"    => "edit_post"
            ),

错误消息是:

Parse error: syntax error, unexpected '(', expecting ')' in E:\my_path\myfile.php on line 18

请注意,它与功能__()标准 wordpress 翻译功能)无关,错误与功能无关,而是SYNTAX。(我过去使用过这个函数数百次,没有任何问题——在这种情况下,同样_x()_e()语法错误也会失败..)

我所有的括号都是封闭的,我已经检查并重新检查过,除非我完全失明,否则我会说没关系,但是无论我将括号放在这个类的哪个位置,我仍然会收到这个错误。

另一个例子:这也会失败并出现同样的错误:

class o99_custom_fields {
                /**
                * @var  string  $prefix  The prefix for storing custom fields in the postmeta table
                */
                var $prefix = 'o99_';
                /**
                * @var  array  $customFields  Defines the custom fields available
                */
                var $dummy_strings = array (
__('x1','text_domain'),
__('x2','text_domain'),
);

    ... more functions and more code ...
        } // End Class

同样,错误似乎是SYNTAX相关的,即使我所有的括号都已关闭。我还检查了文件是否有正确的 php 打开和关闭标签,甚至是字符集和编码(没有 BOM 的 UTF-8)

我以前从未遇到过这样的问题 - 所以任何帮助/提示/见解将不胜感激..

编辑我:

在这些数组之后,是构造函数..

/**
* PHP 4 Compatible Constructor
*/
function o99_custom_fields() { $this->__construct(); }
/**
* PHP 5 Constructor
*/

function __construct() {
    add_action( 'admin_menu', array( &$this, 'createCustomFields' ) );
    add_action( 'save_post', array( &$this, 'saveCustomFields' ) );
}
4

2 回答 2

6

您遇到的问题是因为您无法通过调用其他函数来初始化类属性。

将属性初始化为默认值,如下所示:

class SomeClass{
...
private $myProp0 = array(); //OK
private $myProp1 = array('foo' => 'bar', 'foooo' => 'baaar'); //OK
private $myProp2 = null; //OK
private $myProp3 = 10; //OK
private $myProp4 = "something"; //OK
private $myProp5 = __('translate me') // NOT OK
...
}

要使用其他值初始化您的属性(例如通过调用其他函数),您必须在类的构造函数中设置它。

像这样的东西应该工作:

function someFunction($x, $y){
    return "mouahahaha";
}

class SomeClass{
    private $something = array();

    public function __construct(){
        $this->something = array(
            'somekey1' => 'foobar',
            'somekey2' => someFunction("foo", "bar"),
        );
    }
}

换句话说,您需要将数组初始化从类主体移动到构造函数。

将该示例放入您自己的代码中:

class o99_custom_fields {
        /**
        * @var  string  $prefix  The prefix for storing custom fields in the postmeta table
        */
        var $prefix = 'o99_';
        /**
        * @var  array  $customFields  Defines the custom fields available
        */
         private $customFields = array();
        /**
        * PHP 4 Compatible Constructor
        */
        function o99_custom_fields() { $this->__construct(); }
        /**
        * PHP 5 Constructor
        */

        public function __construct() {

         $this->customFields =  array(

            array(
            "name"          => "some_name",
            "title"         => __("some Title","text_domain"),// NO ERROR NOW
            "description"   => "Some Desctiption Text",
            "type"          => "k_upload",
            "scope"         =>  array( "post" ),
            "capability"    => "edit_post"
        ),
       );
       // Do your other construct things 
     } // END __construct
于 2013-11-02T08:25:54.267 回答
4

我现在注意到你有问题的数组是一个类属性;错误在这里并没有真正的帮助,但是请阅读有关类属性的手册:

[...]这个声明可能包括一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时被评估,并且不能依赖运行时信息才能被评估。

即,__() 函数属于这种情况。事实上,如果它是一个普通的数组定义,它不会抛出错误,看这个ideone

function __($param1,$param2){}

$customFields = array(
   array(
     "name" => "some_name",
      "title" => __("some Title","text_domain"),// ERROR OCCUR
      "description" => "Some Desctiption Text",
      "type" => "k_upload",
      "scope" => array( "post" ),
      "capability" => "edit_post"
    ),
);

使用构造函数初始化属性;此外,该关键字var应替换为显式可见性关键字(此处为public

于 2013-11-02T08:24:07.833 回答