简体   繁体   English

需要一些指导方针以OOP概念重构我的php代码

[英]Need some guideline to refactor my php code in OOP concept

I wrote a wordpress plugin that works fine. 我写了一个wordpress插件,效果很好。 However, it just works but there is no OOP here because at that time it was necessary to build something asap. 但是,它只是可行,但是这里没有OOP,因为当时有必要尽快构建一些东西。 I read some literature and found that php do not support multiple inheritance due to diamond problem. 我阅读了一些文献,发现由于钻石问题,php不支持多重继承。

Current scenario: 当前场景:

Flickr
--pic importer
----1. sql.php
----2. javascript.php
----3. call to show database contents

--photoset importer
----1. sql.php
----2. javascript.php
----3. call to show database contents

Here, I have created 2 class: picImporter and photosetImporter . 在这里,我已经创建了2类: picImporterphotosetImporter Both classes share common contents from ( 1. sql.php and 2. javascript.php ) but point-3 ( implementation of showing database content is differnt for them). 这两个类都共享( 1. sql.php2. javascript.php )的共同内容,但要点3( 显示数据库内容的实现对它们来说是不同的)。

So, my idea is: I should create another class Global and photosetImporter , picImporter class should extend this class. 所以,我的想法是:我应该创建另一个类GlobalphotosetImporterpicImporter类应该扩展此类。 In the Global class there should be an abstract class that child class must define. 在Global类中,应该有一个子类必须定义的抽象类。 So the design becomes: 因此设计变为:

Class Global{
   //$sql comes sql.php,
   //$javacript comes javascript.php,
   abstract protected function showDatabaseContents();
}

Class picImporter extends Global{
    protected function showDatabaseContents() {
        //implementation using **$sql** from base
    }
}

Class photosetImporter extends Global{
    protected function showDatabaseContents() {
        //implementation using **$javascript** from base
    }
}

Before I proceed, I just want to know if I am on right track or not and further instruction if possible. 在继续之前,我只想知道我是否在正确的轨道上,并在可能的情况下提供进一步的指导。

Thanks, -S. 谢谢,-S。

There's no particular "right" way to do what you're looking for (though there are wrong ways). 没有特定的“正确”方法来执行您要寻找的东西(尽管有错误的方法)。 Hard to know what method I would use without understanding what your javascript class does. 在不了解您的javascript类做什么的情况下,很难知道我将使用哪种方法。

Typically, I create a single global DB abstraction class (what I assume your sql class is) and just access it from the global scope wherever I need it. 通常,我创建一个单一的全局数据库抽象类(我假设您的sql类是该类),并且只要需要就可以从全局范围访问它。 Global scope isn't evil, especially for things like database access which aren't inherent to whatever other classes you're creating but are needed pretty much everywhere. 全局作用域并不是邪恶的,尤其是对于数据库访问之类的东西而言,它并不是您正在创建的任何其他类所固有的,但几乎在任何地方都需要。 The same may be true for your javascript class. 您的javascript类也可能如此。

That said, if you need this sort of abstraction to maintain a consistent design in your application, then I see no problem with what you're doing here, this seems like a logical approach. 就是说,如果您需要这种抽象来在应用程序中保持一致的设计,那么我在这里所做的工作没有问题,这似乎是一种逻辑方法。

If you're looking for what might be a best practice, run a search for "PHP design patterns", but in general my approach with PHP is to keep it simple and accessible. 如果您正在寻找最佳实践,请搜索“ PHP设计模式”,但总的来说,我使用PHP的方法是使其简单易用。 That may mean using a design pattern, or it may mean a more basic approach, depending. 这可能意味着使用设计模式,也可能意味着使用更基本的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM