Reputation: 9941
I'm trying to learn a bit of OOP by rewriting a big function into a class. Here is a representation of the most important stuff with relevance to the question
function my_function( $args = [] ) {
$defaults = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
];
$args = wp_parse_args( $args, $defaults );
$q = new WP_Query( $args );
return $q->posts;
}
The user can then use the function as follow
echo my_function( ['key2' => 'new value'] );
key2
here is set with a new value which in turn will alter the output of $q->posts
Now, my question is, how to I make this work in OOP. I had a look at the setters and getters, but does not seem that this is what I'm looking for. I also had a look at how the WP_Query
class does it in Wordpress, but frankly, the coding is quite crappy and very outdated and definitely not something to learn from.
With the WP_Query
class in mind I should have a class like
class My_Class {}
and then I can use it as follow
$output = new My_Class( ['key2' => 'new value'] );
How do I set arguments in a OOP class
Upvotes: 0
Views: 104
Reputation: 14620
The important thing to remember is that classes offer you an easy way to separate out logic into small chunks whilst maintaining functionality in a single wrapper (the created object) that share properties and methods (functions).
To answer your question, you make mention of getters and setters. These are two very useful patterns to use when coding up the class itself. They give you a programmatic way to get and retrieve values that in turn provide you with ways to validate when settings and mutate when getting.
class MyClass {
/**
* Declare a private class property.
* This is useable in scope of THIS class
* and none other, not even extensions of this class.
* has default args populated
**/
private $myArgs = ["key" => "value"];
/**
* Constructor.
**/
public function __construct()
{
// No need for constructor to do anything if args are
// are already assigned by default. Use the setter to
// change them
}
/**
* Get the arguments array
**/
public function getArgs()
{
return $this->myArgs;
}
/**
* Set the arguments array
* Typehint as an array to make sure myArgs will always
* be an array and nothing else.
**/
public function setArgs(array $args)
{
$this->myArgs = $args;
}
}
$myClass = new MyClass();
// Get the args
$args = $myClass->getArgs();
// Set the args to a new value
$myClass->setArgs(["key2" => "value2"]);
// Get the new args
$args2 = $myClass->getArgs();
Upvotes: 1
Reputation: 2186
My suggestion is you wrap WP_Query which does the work for you:
class MyClass {
private $defaults = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
private $wpQuery;
public function __construct($args = array()) {
$this->wpQuery = new WP_Query(wp_parse_args($args, $this->defaults));
}
public function getPosts() {
return $this->wpQuery->posts;
}
}
Upvotes: 1
Reputation: 7795
Your class My_Class should have a constructor like this :
<?php
class My_Class {
private $attr;
function __construct($arg) {
$this->attr = $arg;
}
public function getAttr() {
return $this->attr;
}
}
// test
$obj = new My_Class(array('key2' => 'new value'));
var_dump ($obj->getAttr());
But in PHP, only one constructor is allowed, there is no overload in constructor, functions and methods...
Upvotes: 1
Reputation: 48131
I don't think you need an object for that.
If your routine always needs to return $q->posts
converting your function just to use the methods to pass the arguments it's not a good idea.
Upvotes: 2