English 中文(简体)
Writing refactoring-friendly PHP code
原标题:

As far as I know, and have gathered from other SO posts, there are no proper tools for refactoring PHP code yet, so when it comes to refactoring, it s probably good old search-and-replace for most of us, with a good amount of praying that we didn t overlook something.

I would like to know whether there are any coding recommendations on how to write code friendly for manual refactoring. Never to construct variable names from strings, would be one thing that comes to mind because a construct like that is impossible to grep:

$object->{"field_".$fieldname}

I could imagine there are several such do s and don ts. Maybe somebody knows good resources / articles on the issue. It wouldn t have to be PHP specific, either.

最佳回答

Unit tests always help me identify places where I ve broken code due to a refactor. Unit tests in dynamic languages (PHP, Ruby, Python, etc.) provide assistance where static typing in other languages (Java, C#) would typically allow you to more safely refactor.

问题回答

Avoid magic as much as possible: variable variables, eval, masking errors with @ and storing code in the database will come back to bite you.

Well, The best way to write refactoring friendly code is to write loose coupled ,highly cohesive code and object oriented code.

You should try as much abstraction as you can, after all abstraction is the keyword while programming.

Moroever, you should be layering your code into presentation layer, business layer, data layer etc.. and Using design patterns is a pretty good solution.

I d recommend you to read Martin Fowler.

Your question makes a certain amount of sense. But—at the same time—it implies that the implementation is known to be inadequate, and written to be replaced. Why not just architect it properly the first time?

First, make sure your variable names make sense. If possible, go as OOP as you possibly can, or at least keep everything organized (image function file, database file, etc)

Second, and this is handy, check your IDE. Netbeans has options for refactoring. You can search in a file, in a folder, in a project, etc.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签