简体   繁体   English

PHP多个类,哪个继承,接口,抽象?

[英]PHP multiple classes, Which inherit, interface, abstract?

I understand basic PHP classes and inheritance, but these don't solve in a elegant way what I want. 我了解基本的PHP类和继承,但是这些不能以一种优雅的方式解决我想要的问题。 I have been reading articles and watching video trying to get to grips with more advance OOP. 我一直在阅读文章和观看视频,以期更深入地了解OOP。 But for the life of me I can not seam to find a neat solution for the following project. 但是对于我的一生,我无法为下一个项目找到一个巧妙的解决方案。 Any advice or hints? 有什么建议或提示吗?

I have a web application that is basically a fancy inventory and inspection record. 我有一个基本上是精美的库存和检验记录的Web应用程序。 One page has a sortable datatable, pop up dialog boxes and radio buttons to alter an items status. 一页具有可排序的数据表,弹出对话框和单选按钮,用于更改项目状态。 The page has classes for 该页面具有用于

baseClass = basic page construction, sets database connection and environment values
A dataTable class
A class that generates javascript and jquery code 
A class for managing status radio buttons
Classes and methods for updating the database

The problem is how to code these different class so they can be used on other pages and can be maintained/tweaked simply. 问题是如何编码这些不同的类,以便可以在其他页面上使用它们,并可以对其进行简单地维护/调整。 At the moment the classes are called as and when needed using spl_autoload_register() but i get stuck ideas about Singleton, Dependancy Injection, Factories. 目前,这些类在需要时使用spl_autoload_register()进行了调用,但是我对Singleton,依赖注入,工厂有一些想法。

Any advice? 有什么建议吗? (I am sure its a simple question, but banging my head against the wall is hindering me) (我确信这是一个简单的问题,但将我的头撞到墙上会阻碍我前进)

Thanks in advance 提前致谢

You can try Composer . 您可以尝试Composer

It is very easy and simple (all though, I didn't get a grip how to set up just a simple autoload, from their website). 这是非常容易和简单的(尽管如此,我从他们的网站上并没有掌握如何设置简单的自动加载的功能)。 To set up simple autoload I used this tutorial: http://jessesnet.com/development-notes/2014/php-composer-autoloading/ 为了设置简单的自动加载,我使用了本教程: http : //jessesnet.com/development-notes/2014/php-composer-autoloading/

Also read through PHPBridge it will give you good idea how to set up your classes and later use them with Composer. 还要通读PHPBridge ,它将为您提供一个很好的主意,即如何设置您的类以及以后将其与Composer一起使用。

Don't try to reuse classes, try to think in interfaces. 不要尝试重用类,而要考虑接口。 Interfaces are behavioral contracts. 接口是行为契约。 They should be standard accross pages, implementations might vary. 它们应该是跨页面的标准,实现可能会有所不同。

For instance, you have an interface for getting the data. 例如,您有一个获取数据的接口。 In practice might you use a mock class (ie hardcoded data in the class to test other parts of your application), you might get data from MySQL or from PostgreSQL or from any other source. 实际上,您可能使用模拟类(即,该类中的硬编码数据来测试应用程序的其他部分),则可能从MySQL或PostgreSQL或任何其他来源获取数据。 All different implementations of the same interface. 相同接口的所有不同实现。 The interfaces should be able to communicate with each other, not the implementations themselves unless it is through their interfaces. 接口应该能够彼此通信,而不是实现本身,除非通过它们的接口通信。

I would advice you to create this code yourself. 我建议您自己创建此代码。 It will give you the knowledge to understand frameworks better (in the long run). 从长远来看,它将为您提供更好地理解框架的知识。

I prefer the choice for implementation by configuration over auto-wiring, because it gives me more flexibility. 与自动装配相比,我更喜欢通过配置来实现,因为它给了我更大的灵活性。 In design patterns: I prefer a service factory over dependency injection, but both are good solutions. 在设计模式中:我比依赖注入更喜欢服务工厂,但是两者都是很好的解决方案。

Thanks everyone, As the time limit on this project is generous I'l spend a bit of time looking at design patterns again (I've only ever encountered structured patterns when editing existing projects) and look to doing the code myself as this is a secondhand project and starting from scratch is not really an option (yet!) 谢谢大家,由于这个项目的时间有限,我将花一些时间再次研究设计模式(我在编辑现有项目时只遇到过结构化模式),并希望自己编写代码,因为这是一个二手项目并从头开始并不是真正的选择(但!)

Once more thanks for your excellent advice. 再次感谢您的出色建议。

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

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