我有一个像这样的简单代码:
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' ) );
}
您遇到的问题是因为您无法通过调用其他函数来初始化类属性。
将属性初始化为默认值,如下所示:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句