Php 有人能给我解释一下mysqli';s类方法有自己的子方法吗?
例如:Php 有人能给我解释一下mysqli';s类方法有自己的子方法吗?,php,oop,methods,properties,extends,Php,Oop,Methods,Properties,Extends,例如: $m = new mysqli('host', 'user', 'pass', 'db'); $q = $m->query('SELECT stuff FROM table'); while ($row = $q->fetch_assoc()) { // do stuff } query方法如何拥有自己独特的“子方法”,如fetch\u assoc() 如何使用OOP复制此行为 编辑。。。这是否被视为正确和/或良好做法 class MyClass {
$m = new mysqli('host', 'user', 'pass', 'db');
$q = $m->query('SELECT stuff FROM table');
while ($row = $q->fetch_assoc()) {
// do stuff
}
query
方法如何拥有自己独特的“子方法”,如fetch\u assoc()
如何使用OOP复制此行为
编辑。。。这是否被视为正确和/或良好做法
class MyClass {
function myMethod() {
return new AnotherClass();
}
}
class AnotherClass {
function __construct() {
$this->stuff = 'stuff';
}
}
然后,我可以做:
$obj = new MyClass;
$stuff_getter = $obj->myMethod();
echo $stuff_getter->stuff;
这是一个有点意见的基础上,你问什么,但因为我认为很多人都在挣扎这一点,我会提供一些一般信息,关于这个案件。 首先,你不能说这是不是一个好的做法。这取决于意图和背景。请参阅此代码:
class MyClass {
function myMethod() {
return new AnotherClass();
}
}
可以吗?是的,你在做什么没关系,但是请注意,你在这里有很强的依赖性。如果您出于某种原因想要有不同的实现
对于另一个类,您需要更改代码。
您可以通过使用依赖项注入来防止这种情况。在AnotherClass
中实现一个接口,并将其注入MyClass
。
当您有另一个类的实现时,您可以像传递“旧”版本一样传递它。
如何实现这一点也取决于代码的意图,但我将提供代码的基本示例
class MyClass {
private $aClass = null;
function __construct($aClass)
{
$this->aClass = $aClass;
}
function myMethod() {
return new $this->aClass();
}
}
interface AnInterface
{
}
class AnotherClass implements AnInterface {
function __construct() {
$this->stuff = 'stuff';
}
}
$obj = new MyClass(new AnotherClass());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->stuff;
现在我可以使用它创建另一个类的实现,并像往常一样将它传递给MyClass
。而不是你的场景,我需要添加另一个函数。例如:
class AnotherClass2 implements AnInterface {
function __construct() {
$this->stuff = 'another stuff';
}
}
$obj = new MyClass(new AnotherClass2());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->stuff;
我注意到的第二件事是你没有定义变量。我认为这有点基于观点,但我强烈反对公共变量(在您的案例中,这是默认情况)。
创建一个变量并在构造函数中分配该变量。创建getter和setter(如果您懒惰,可以创建magic getter en setters()。您将得到如下结果:
class AnotherClass implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
$obj = new MyClass(new AnotherClass());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
class MyClass {
private $aClass = null;
function __construct($aClass)
{
$this->aClass = $aClass;
}
function myMethod() {
return new $this->aClass();
}
}
interface AnInterface
{
public function getStuff();
}
class AnotherClass implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
class AnotherClass2 implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'another stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
$obj = new MyClass(new AnotherClass());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
$obj = new MyClass(new AnotherClass2());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
我希望这能让你的结构更清晰,尽管这可能不能完全回答你的问题
关于这一点有两个注释
该接口在PHP中并不总是必需的,但它确实是最佳实践
如果有很多重复,当然可以使用继承(是一种关系吗?),而不是实现接口,也可以使用继承结构
您的最终代码可以是(作为接口示例)如下所示:
class AnotherClass implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
$obj = new MyClass(new AnotherClass());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
class MyClass {
private $aClass = null;
function __construct($aClass)
{
$this->aClass = $aClass;
}
function myMethod() {
return new $this->aClass();
}
}
interface AnInterface
{
public function getStuff();
}
class AnotherClass implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
class AnotherClass2 implements AnInterface {
private $stuff;
function __construct() {
$this->stuff = 'another stuff';
}
public function getStuff()
{
return $this->stuff;
}
}
$obj = new MyClass(new AnotherClass());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
$obj = new MyClass(new AnotherClass2());
$stuff_getter = $obj->myMethod();
echo $stuff_getter->getStuff();
因为查询返回一个mysqli\u结果
对象——我怀疑这是一个“阅读手册”的例子:一个方法很少返回自己,它返回像对象、字符串、数组等数据结构。$m
和$q
在您的例子中是对象,分别从mysqli::\uu construct()返回
和mysqli::query()
@RichardBernards:我以为构造函数方法无法返回值…?我如何使用OOP复制这种行为?这是一个对象!!!在这方面可以使用@CD001。公平地说,我一直在阅读PHP OOP,我只需要稍微了解一下。