Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP (A few questions) OO, refactoring, eclipse

I am using PHP in eclipse. 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). How are people dealing with this?

  2. How are people documenting their code - in particular the required GET and POST data?

Secondly OO Development:

  1. Should I be going full OO in my development. 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?

  3. How should I name my files, should I use all lower case with _ for spaces "hello_world.php"? Should I name classes with Uppercase like 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.

  2. Are there any plugins to improve php refactoring? I am using the official PHP version of Eclipse from their website.

thanks

like image 499
jax Avatar asked Nov 06 '22 12:11

jax


1 Answers

Firstly General Coding

1) OO can help you with that. 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. (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". PHPdoc is a good start; will help to create API documentation.

Secondly OO Development:

3) As said before -- "it depends". Do it when needed. You don't have to go full OO for "hello world". 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. 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". 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. 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. Symfony, e.g., uses CamelCase like Java.

Thirdly Refactoring

I must say this is a real pain.

yes :3 But it pays off.

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.

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. ;)

But as for how to prevent it -- be smart. Honour program contracts, i.e. 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; 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. Though I am sure for the doable cases, there should be plugins. :)

like image 151
Cornelius Avatar answered Nov 11 '22 06:11

Cornelius