Php OOP中的参数
我试图通过将一个大函数重写为一个类来学习一点OOP。以下是与问题相关的最重要的内容Php OOP中的参数,php,wordpress,oop,Php,Wordpress,Oop,我试图通过将一个大函数重写为一个类来学习一点OOP。以下是与问题相关的最重要的内容 function my_function( $args = [] ) { $defaults = [ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ]; $args = wp_parse_args( $args, $defaults ); $
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;
}
然后,用户可以使用以下功能
echo my_function( ['key2' => 'new value'] );
$output = new My_Class( ['key2' => 'new value'] );
key2
此处设置了一个新值,该值反过来将改变$q->posts的输出
现在,我的问题是,如何在OOP中实现这一点。我看了一下二传手和传接手,但似乎这不是我想要的。我还研究了WP\u Query
类在Wordpress中是如何实现的,但坦率地说,代码编写非常糟糕,非常过时,绝对不值得学习
考虑到WP\u查询
类,我应该有一个类似
class My_Class {}
然后我可以使用它如下
echo my_function( ['key2' => 'new value'] );
$output = new My_Class( ['key2' => 'new value'] );
如何在OOP类中设置参数我认为您不需要对象来实现这一点。
如果你的例程总是需要返回
$q->posts
转换你的函数只是为了使用方法来传递参数,这不是一个好主意。我认为你不需要一个对象
如果你的例程总是需要返回
$q->posts
转换你的函数只是为了使用方法传递参数,这不是一个好主意。你的类我的类应该有这样一个构造函数:
<?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());
您的类我的类应该有这样一个构造函数:
<?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());
需要记住的重要一点是,类为您提供了一种简单的方法,可以将逻辑分成小块,同时在共享属性和方法(函数)的单个包装器(创建的对象)中维护功能
为了回答你的问题,你提到了能手和二传手。在编写类本身的代码时,可以使用这两种非常有用的模式。它们为您提供了一种获取和检索值的编程方式,从而为您提供了在获取设置时进行验证和在获取设置时进行变异的方法
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();
需要记住的重要一点是,类为您提供了一种简单的方法,可以将逻辑分成小块,同时在共享属性和方法(函数)的单个包装器(创建的对象)中维护功能
为了回答你的问题,你提到了能手和二传手。在编写类本身的代码时,可以使用这两种非常有用的模式。它们为您提供了一种获取和检索值的编程方式,从而为您提供了在获取设置时进行验证和在获取设置时进行变异的方法
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();
我的建议是,您可以使用WP_查询完成以下工作:
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;
}
}
我的建议是,您可以使用WP_查询完成以下工作:
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;
}
}
哈明白你的意思了。没有真正考虑到这一点。尽管如此,我还是很高兴认识你!明白你的意思了。没有真正考虑到这一点。尽管如此,如果您的代码几乎可以正常工作,那将是一个很好的了解。我的问题是,如何在类中设置默认参数,然后用用户指定的参数覆盖它们。还是让我的功能保持原样更好?我会在周末测试这个,谢谢。从今天起将非常繁忙:-)您的代码几乎可以正常工作。我的问题是,如何在类中设置默认参数,然后用用户指定的参数覆盖它们。还是让我的功能保持原样更好?我会在周末测试这个,谢谢。从今天起将非常忙:-)代码中有一个解析错误。语法错误,意外的“getAttr”(T_字符串)代码中存在分析错误。语法错误,意外的'getAttr'(T_字符串)非常感谢,这正按照我的预期工作。:-)如果你能多次投票,我会再次投票给你:-)非常感谢,我希望它能像我需要的那样工作如果你能多次投票,你会再次投票支持你:-)