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();
I really like your (real) code and it’s usually kinda hard for me to like other persons code (I haven’t had much time to dig into ZF but PEAR for instance [they also have their coding standards] is just awful IMO), the first example you gave just seems stupid but regarding the second one, it is really easy to understand at least for me and from the short snippet you provided you seem to have a consistent coding style and be using whitespaces in the right amount and at right places – that counts a lot for clean code (if you don’t believe me just take a look at some Perl snippets).
I would only like to point out three things:
rSumis not a awful name for a property it isn’t quite clear what values does it hold, perhaps you could find a more descriptive name for that property?Your second “real” example could be cleaner and faster if you use the right functions:
$this->rSum = array_flip(explode(“,”, $this->options[“rSum”]));
EDIT: I just noticed the code I provided above is not quite what you’re doing (the
0was not processed by my brain), here is another working alternative:There seems to be a lot of people here that don’t like one-liners I however, believe that the the above code is clear, efficient and descriptive – but that may be just me… =)