Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and writing clean code

Im trying to find best practices to write PHP.

I just wonder is this a bad habit.

For example, processing variables.

$var = 1
$var = doSomething($var);
$var = doSomething2($var);
$var = doSomething3($var);

It looks a bit awful.

Here is a example of a real code that I just did:

$this->rSum = explode(",", $this->options["rSum"]);
$this->rSum = array_combine(array_values($this->rSum), array_fill(0, count($this->rSum), 0));

If someone could pass me some good tutorials of writing cleaner code generally it would be nice!

Its me again asking stupid questions. :)


By the way..

How much automatic processing can there be in models?

I have a model that has a execute method and when I call it, it does a lot of things like reading a definition file and making database queries.

For example

$object = new Object()
$object->setFile("example.txt");
$object->execute();

// Then i can fetch things from it
echo $object->getName();
like image 646
PPPHP Avatar asked May 05 '10 06:05

PPPHP


2 Answers

Smart code is not necessarily good code in my opinion. I'd personally prefer clean, simple and easy to understand code. Your 2 liner will make your peer think hard, as opposed to your init "bad" code.

That's just my take anyway

like image 158
Jim Li Avatar answered Oct 05 '22 12:10

Jim Li


  1. Zend Framework Coding Standard
  2. PEAR Coding Standard

The most important, be consistent.

like image 31
Boris Guéry Avatar answered Oct 05 '22 13:10

Boris Guéry