简体   繁体   English

PHP(几个问题)OO,重构,eclipse

[英]PHP (A few questions) OO, refactoring, eclipse

I am using PHP in eclipse. 我在eclipse中使用PHP。 It works ok, I can connect to my remote site, there is colour coding of code elements and some code hints. 它工作正常,我可以连接到我的远程站点,有代码元素的颜色编码和一些代码提示。

I realise this may be too long to answer all questions, if you have a good answer for one part, answering just that is ok. 我意识到这可能太长了,无法回答所有问题,如果你对一个部分有一个很好的答案,回答就是这样。

Firstly General Coding 首先是一般编码

  1. I have found that it is easy to loose track of included files and their variables. 我发现很容易丢失包含文件及其变量的跟踪。 For example if there was a database $cursor it is difficult to remember or even know that it was declared in the included file (this becomes much worse the more files you include). 例如,如果存在数据库$ cursor,则很难记住甚至知道它已在包含的文件中声明(这会使您包含的文件越多,这就越糟糕)。 How are people dealing with this? 人们如何处理这个问题?

  2. How are people documenting their code - in particular the required GET and POST data? 人们如何记录他们的代码 - 特别是所需的GET和POST数据?

Secondly OO Development: 其次OO开发:

  1. Should I be going full OO in my development. 我是否应该在我的开发中完全使用OO。 Currently I have a functions library which I can include and have separated each "task" into a separate file. 目前我有一个函数库,我可以包含它,并将每个“任务”分成一个单独的文件。 It is a bit nasty but it works. 它有点讨厌,但它的工作原理。

  2. If I go OO how do I structure the directories in PHP, java uses packages - what about php? 如果我去OO如何在PHP中构建目录,java使用包 - 那么php呢?

  3. How should I name my files, should I use all lower case with _ for spaces "hello_world.php"? 我应该如何命名我的文件,我是否应该使用_为空格“hello_world.php”的所有小写? Should I name classes with Uppercase like Java "HelloWorld.php"? 我应该用像Java“HelloWorld.php”这样的大写字母来命名吗? Is there a different naming convention for Classes and regular function files? 是否有不同的类和常规函数文件的命名约定?

Thirdly Refactoring 第三,重构

  1. I must say this is a real pain. 我必须说这是一个真正的痛苦。 If I change the name of a variable in one place I have to go through whole document and each file that included this file and change the name their too. 如果我在一个地方更改变量的名称,我必须浏览整个文档和包含此文件的每个文件,并更改它们的名称。 Of course, errors everywhere is what results. 当然,到处都是错误。 How are people dealing with this problem? 人们如何处理这个问题? In Java if you change the name in one place it changes everywhere. 在Java中,如果您在一个地方更改名称,它会随处更改。

  2. Are there any plugins to improve php refactoring? 有没有插件来改善php重构? I am using the official PHP version of Eclipse from their website. 我在他们的网站上使用Eclipse的官方PHP版本。

thanks 谢谢

I've found using a PHP framework (eg Zend , Cake , CodeIgniter , etc) can force class structures and naming conventions while generally addressing autoloading as well. 我发现使用PHP框架(例如ZendCakeCodeIgniter等)可以强制类结构和命名约定,同时通常也可以解决自动加载问题。 Using PHPDoc formatting liberally helps with code-completion and hinting as well as documenting specific requirements (eg method parameter definitions). 使用PHPDoc格式化有助于代码完成和提示以及记录特定需求(例如方法参数定义)。

Firstly General Coding 首先是一般编码

1) OO can help you with that. 1)OO可以帮助你。 As you encapsulate variables and functionality, they don't go out and mess with the namespaces. 在封装变量和功能时,它们不会出现并混淆命名空间。 Assumind I understand right what problem you hint at, using an OO approach helps alleviating conflicts that can arise when you are inadvertedly redeclaring varables. Assumind我理解你提示的问题,使用OO方法有助于缓解当你无意中重新声明可变数据时可能出现的冲突。 (Note: Alleviate. Not completely prevent on its own. ;)) Otherwise a practise i have encounterd is prepending variable names with something like a 'package name' -- which merely shifts the problem one level up and isn't exactely beautiful either. (注意:缓解。不完全防止它自己。;))否则我遇到的一种做法是将变量名称添加到类似“包名称”之类的东西 - 这只是将问题转移到一级并且不是非常漂亮。 :| :|

2) "However suits their purpose". 2)“但不适合他们的目的”。 PHPdoc is a good start; PHPdoc是一个好的开始; will help to create API documentation. 将有助于创建API文档。

Secondly OO Development: 其次OO开发:

3) As said before -- "it depends". 3)如前所述 - “它取决于”。 Do it when needed. 在需要时做。 You don't have to go full OO for "hello world". 你不必为“hello world”充分展示OO。 But you can. 但是你可以。 Weigh the costs and benefits of either route and choose wisely. 权衡任何一条路线的成本和收益,并明智地选择。 Though I personally want to suggest when in doubt favour OOP over 'unstructured' approaches. 虽然我个人想建议在有疑问时支持OOP而不是'非结构化'方法。 Basically, know your tools and when to use them -- then you can make that call on your own easily. 基本上,了解您的工具以及何时使用它们 - 然后您可以轻松地自行拨打电话。 :) :)

4) As far as I can see, the directories "are structured like packages". 4)据我所知,目录“的结构类似于包”。 Mind you, "directories" and "like". 请注意,“目录”和“喜欢”。 Having said that, various frameworks have solved that problem for theirselves; 话虽如此,各种框架已经为自己解决了这个问题; cf; 比照; th eother answers. 第二个答案。

5) Again, however you please. 5)不过,不过请你。 There is not a definitive way You Have To Do It Or Else. 没有一种明确的方式你必须要做或者做其他事。 Just stick to it once you chose your path ;3 Aside of that certain frameworks etc. have their own naming conventions. 一旦你选择了你的路径就坚持下去; 3除了某些框架等之外,还有自己的命名约定。 Symfony, eg, uses CamelCase like Java. 例如,Symfony使用像Java这样的CamelCase。

Thirdly Refactoring 第三,重构

I must say this is a real pain. 我必须说这是一个真正的痛苦。

yes :3 But it pays off. 是的:3但它还是有回报的。

If I change the name of a variable in one place I have to go through whole document and each file that included this file and change the name their too. 如果我在一个地方更改变量的名称,我必须浏览整个文档和包含此文件的每个文件,并更改它们的名称。 Of course, errors everywhere is what results. 当然,到处都是错误。 How are people dealing with this problem? 人们如何处理这个问题? In Java if you change the name in one place it changes everywhere. 在Java中,如果您在一个地方更改名称,它会随处更改。

No, it doesn't. 不,它没有。 If you get yourself a tool with support you only have to use the refactoring tool once; 如果你给自己一个支持的工具,你只需要使用一次重构工具; but if you rename a class property in java, there is no magic bot that walks through the internet and automagically makes sure everyone on the planet uses the new name. 但是如果你在java中重命名一个类属性,那么没有神奇的机器人可以通过互联网自动确保地球上的每个人都使用新名称。 ;) ;)

But as for how to prevent it -- be smart. 但至于如何防止它 - 聪明。 Honour program contracts, ie use interfaces. 荣誉计划合同,即使用接口。 Do not use functions / members you shouldn't use directly. 不要使用不应直接使用的函数/成员。 Watch the hierarchies. 观察层次结构。 Use a reasonable division of code and respect this division's boundaries. 使用合理的代码划分并尊重该划分的界限。

But how people deal with that problem? 但人们如何处理这个问题呢? Well, search and replace I suppose ;) 好吧,我猜想搜索和替换;)

As for the Eclipse-Plugin -- The dynamic nature of PHP makes it more difficult to automagically refactor code; 至于Eclipse-Plugin - PHP的动态特性使得自动重构代码变得更加困难; we can't always use static type hinting etc., and divination of argument and return types is impossible more often than not. 我们不能总是使用静态类型提示等,并且参数和返回类型的占卜通常是不可能的。 So, to the extent of my knowledge, 'automatic refactoring' is not as well-supported by tools as in the Java world. 因此,据我所知,“自动重构”并不像Java世界那样得到工具的支持。 Though I am sure for the doable cases, there should be plugins. 虽然我确信可行的案例,但应该有插件。 :) :)

For the OO Development part: 对于OO开发部分:

I am using the autoload functionality to load the classes dynamically. 我正在使用自动加载功能动态加载类。 My directory structure is like packages in java. 我的目录结构就像java中的包。 My classes are named like in java (eg HelloWorld.php). 我的类在java中命名(例如HelloWorld.php)。 But the class is defined with the complete path to that class (eg class FW_package1_package2_HelloWorld {...}). 但是该类定义了该类的完整路径(例如,类FW_package1_package2_HelloWorld {...})。

If a class is called the autoload method replaces all _ against / and searches for the class with the extracted path (eg FW/package1/package2/HelloWorld.php). 如果一个类被调用,则autoload方法将所有_替换为/并使用提取的路径搜索该类(例如,FW / package1 / package2 / HelloWorld.php)。

I am strongly influenced by Java, so that I chose this way. 我受Java的强烈影响,因此我选择了这种方式。

Take a look at nWire for PHP . 看看nWire for PHP It is a plugin for Eclipse PDT which provides code exploration and visualization. 它是Eclipse PDT的一个插件,提供代码探索和可视化。

It can easily be used to trace dependencies within your application and it is very handy for OO projects, enabling you to visualize class hierarchies and much more. 它可以很容易地用于跟踪应用程序中的依赖项,对于OO项目非常方便,使您可以可视化类层次结构等等。

It doesn't support refactoring, but it can assist by showing you the references of a given components (eg a function or a field). 它不支持重构,但它可以通过向您显示给定组件(例如函数或字段)的引用来提供帮助。

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

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