Jump to content

php class troubles

Judahnator
Go to solution Solved by Cheesebaron,

Can't remember why this does not work in PHP, but I know how to work your way around it.

 

Classes have something called a constructor, which is called when you instantiate a new instance of that Class. In there you can call that function get_option and get the date format.

 

So code like this would work:

 

class JSON_API_Query {    protected $defaults = array(        'date_format' => 'F j, Y',        'read_more' => 'Read more'        );            function __construct() {        $format = get_option('date_format');        $this->defaults['date_format'] = $format;    }}

You can test it here: http://goo.gl/CNW9v8

I am working on an API plugin for a Wordpress installation, and i have some old code to work with.
 
Here is my code:

class JSON_API_Query {  protected $defaults = array(    'date_format' => "F j, Y",    'read_more' => 'Read more'  );

This just formats some stuff later. You can see the "date_format" is statically assigned. I would like to change that to something dynamically pulled from a database.

 

What i would like to do is something like this:

class JSON_API_Query {  protected $defaults = array(    'date_format' => get_option('date_format'),    'read_more' => 'Read more'  );

(for those who dont use Wordpress, the get_option() function is one of the database interfaces)

However, whenever i try to do this i get fatal errors. So, how would i go about inserting a variable into this array?

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

Can't remember why this does not work in PHP, but I know how to work your way around it.

 

Classes have something called a constructor, which is called when you instantiate a new instance of that Class. In there you can call that function get_option and get the date format.

 

So code like this would work:

 

class JSON_API_Query {    protected $defaults = array(        'date_format' => 'F j, Y',        'read_more' => 'Read more'        );            function __construct() {        $format = get_option('date_format');        $this->defaults['date_format'] = $format;    }}

You can test it here: http://goo.gl/CNW9v8

Link to comment
Share on other sites

Link to post
Share on other sites

 

Can't remember why this does not work in PHP, but I know how to work your way around it.

 

Classes have something called a constructor, which is called when you instantiate a new instance of that Class. In there you can call that function get_option and get the date format.

 

So code like this would work:

 

class JSON_API_Query {    protected $defaults = array(        'date_format' => 'F j, Y',        'read_more' => 'Read more'        );            function __construct() {        $format = get_option('date_format');        $this->defaults['date_format'] = $format;    }}

You can test it here: http://goo.gl/CNW9v8

 

 

You sir, are a lifesaver!

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×