Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - strange syntax errors with brackets inside Class array

I have a simple code like so :

        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

And everything seems ok,

The problem begins when I am trying to change some array values , and put them inside Brackets ()

for example :

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

The Error message is :

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

Note that it is not related to the function __() ( standard wordpress translation function ) and the error is not function related , but SYNTAX. ( I have used this function hundreds of times in the past , without any problems - and in this case , also _x() and _e() fail on the same syntax errors .. )

All my brackets are closed , I have checked and re checked , and Unless I am totally blind , I would say that it is ok , but I still getting this error , no matter where I put the brackets inside this class .

Another example : this will also fail with the same error :

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

Again, the error appears to be SYNTAX related , even though all my brackets are closed . I have also checked the file for proper php opening and closing tags , and even charset and encoding ( UTF-8 without BOM )

I have never encountered such a problem before - so any help / hint / insight would be greatly appreciated ..

EDIT I :

After those arrays, comes the constructors ..

/**
* 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' ) );
}
like image 456
Obmerk Kronen Avatar asked Dec 26 '22 18:12

Obmerk Kronen


1 Answers

The problem you're encountering is because you can't initialize class properties by calling other functions.

Initializing a property to a default value like this:

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
...
}

To initialize your property with some other value (e.g. by calling some other function) you must set it in the constructor of your class.

Something like this should work:

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

class SomeClass{
    private $something = array();

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

In other words , You need to move your array initialization from the class body to a constructor.

Putting that example to your own code :

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
like image 150
Christian P Avatar answered Dec 28 '22 10:12

Christian P