Hot answers tagged

10

It's a matter of perspective. Writing tests for our code forces us to decouple the code. You COULD view this as "code that exists only for the tests", but I think this is misleading. What you're actually doing is decoupling your code. You are removing hard dependencies and getting rid of the headaches that come with code that news up everything it needs. ...


8

I'd prefer Option #4: Avoid relying on inheritance. Just make UrlBuilder an interface, and give people some way to inject their own implementation to the process, overriding the default you provided. interface UrlBuilder { public function url($config); } class DefaultUrlBuilder implements UrlBuilder{ public function url($config) { $config = ...


4

If you see someone putting different "things" into a single database table using a delimiter to keep them separate, that's a problem. It could be that someone does not have permission to change the database structure or that they just don't "get" databases. Either way, it's a performance and maintenance headache. In your situation I would probably ...


3

Have an abstract base class and two concrete derived classes. You didn't tell us what is different, but only said "their implementations are almost the same with each other". Let's assume the database name is the only difference: abstract class Model_base { public function insert($tableName, $columns, $preds = null) { $db = $this->getDB(); ...


3

If you are shifting x right by b bits (x >> b), then the least significant b bits will be lost. To catch them before they're lost, you can use the bitwise and operation with a mask that contains the same number of 1 bits as you are shifting (i.e. 1 for 1 bit, 3 for 2 bits, 7 for 3 bits, 0xF for 4 bits, ... ((1 << b) - 1) for b bits). E.g lost = num ...


3

The bitwise shift-left (<<) and shift-right (>>) in higher level languages such as PHP, C or C++ lose the most or least significant bit. However, with many CPUs, the assembly instruction for shifting doesn't really lose this bit but shift it into the carry forward flag. So you could then react on this carry flag, as explained in this SO ...


2

The main issue here is that data coming from the client cannot be trusted. So you need to be concerned about replay attacks and people tampering your messages. Tamper-protection I have actually implemented a real-world client for a payment service very similar to what you have described, and for tamper protection it used a message authentication code (MAC) ...


2

If you must really go that way (sometimes it's a requirement imposed by the "up-aboves"). you could create long explicit String codes ie : ERROR__PROJECT_COULD_NOT_CREATE_THAT_UBER_IMPORTANT_RESOURCE and through a deterministic hashing function turn it into a 32 bit integer. Ideally to be global you should probably have some sort of centralized ...


2

Technically, there is no quicker or slower alternative: it's the same request to the same resource, and the processing and sending of the image would probably take much more time than the displaying on client side. The only difference is the perception by the user, with a major difference between <img> and background-image. If <img> doesn't ...


1

A good rule is to avoid having objects that are in a illegal state. Therefore creating objects based on data from the database that is illegal (or indeed has become illegal because you have changed the rules) without anyone knowing about it would be bad. It would of course also be bad if noone were able to start your system because one record somewhere ...


1

Just create a new instance of PDO: $connectionOne = new PDO(" --- dsn --- ", $username, $password, [ PDO::ATTR_PERSISTENT => true ]); $connectionTwo = new PDO(" --- another dsn ---", $anotherUser, $anotherPassword); Just take a look here and here for your driver-specific options.


1

Always use a separate linked table for storing “multiple x for each y” type data. First normal form requires that each field in a row contain an atomic value – that is, a value which is not divisible into multiple sub-values. In case you’re wondering just how useful a separate table will be, compared to a comma-separated list in a single column, consider ...


1

The question is already a bit aged, but maybe someone still stumbles upon it. Most of the mvc-based PHP frameworks offer a feature called scaffolding. With this feature you can easily create simple masks for CRUD operations. Instead of ids ususally a display-field is used for defining relations. I often use CakePHP to achieve what you describe, but it ...


1

The examples shown is correct, but Zend\Form is not exactly what you want to use in your controllers. I'm talking about Exhibit A, where it shows how much code to create, populate and style a form with two elements. It is tedious to create forms that way. To avoid turning the controller into a form factory, you need to create a factory that will create the ...


1

I have answered this question on StackOverflow as well - I place my answer here for easy reference... The PRG pattern alone will not prevent this, as the P action in it takes time (which is usually the case) and the user can submit the form again (via click or browser refresh), which will cause the PRG pattern to "fail". Note that malicious users can also ...



Only top voted, non community-wiki answers of a minimum length are eligible