我有一个简单的代码:
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
请注意,它与函数__()
(standard wordpress translation function)无关,且错误与函数无关,但SYNTAX
。 (我过去曾使用过这个函数数百次,没有任何问题 - 在这种情况下,_x()
和_e()
也会因相同的语法错误而失败。)
我的所有括号都已关闭,我已检查并重新检查,除非我完全失明,否则我会说它没问题,但无论我把括号放在这个类中,我仍然会收到此错误。< / p>
另一个例子:这也会因同样的错误而失败:
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打开和关闭标签,甚至是字符集和编码(UTF-8没有BOM)
我之前从未遇到过这样的问题 - 所以任何帮助/提示/见解都会受到高度赞赏..
编辑我:
在那些数组之后,来自构造函数..
/**
* 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' ) );
}
答案 0 :(得分: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
答案 1 :(得分:4)
我现在注意到你的有问题的数组是一个类属性;这个错误在这里并没有真正帮助,而是阅读class properties上的手册:
[...]此声明可能包括初始化,但是这个 初始化必须是一个常量值 - 也就是说,它必须能够 在编译时进行评估,不得依赖于运行时 信息以便进行评估。
即,__()函数属于这种情况。实际上,如果它是正常的数组定义,则不会抛出错误,请参阅此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
)