Tag Info

New answers tagged

3

While there are no hard rules about the size of a controller, 5000 lines is clearly beyond any reasonable size for a hand-coded class. Chances are this class is violating the Single Responsibility Principle (SRP) and therefore is flawed. You most likely have several methods in there that can logically be grouped together. See if you can't extract methods off ...


1

Lets assume index.php as your home page. I can assume that your controller might work using a action parameter appended to the URL or based on request parameter set. index.php?search would takes the controller to execute search logic and there you get all post data of search criteria. and loads products based on a query mapped with data keywords entered by ...


1

Your approach is far different from that of most PHP applications does, obviously a template engine binds some logic in views, most programmers prefer writing this logic in PHP itself. Limiting PHP to create services/ API s to only manipulate Data is a good idea, indeed. You may consider http://backbonejs.org/. I believe you could use it to write all ...


0

Depending on the problem, if it includes data or collections it is usual that you may come up with a solution using arrays. But if you're thinking that it's the programming language's fault, then think again; problem is the way of how you think solutions.


0

First, check if $id is a valid numeric value ( using is_numeric ) then finally cast the $id to type ( int ).


1

You can: Use polling However, think about sending a request every 10 seconds. By using your suggested approach, you are keeping a long connection to the server, which is in turn keeping a busy process/thread. If this is used only by you, it's fine. If it's meant to be used by many users, it's a huge waste of resources, since the server will be busy doing ...


0

The reason you are probably not finding those examples is because that would mean implementing a complete templating system in PHP, which is far beyond the scope of a lot of tutorials. A good templating engine does not mingle PHP into the view, but there will always be binding logic in your views. There are plenty of templating frameworks out there and ...


1

First of all, I don't think Composer is trying too hard to be a PHP version of NPM - there may be similarities, but differences are to be expected. It may not be the best way to understand it to think of the tool as NPM, Maven, Rake or whatever, but translated into PHP. Secondly, understand that Composer solves 'problem' of autoloading in PHP. Having two ...


1

Composite often goes hand in hand with Builder pattern to address exactly the problem you have mentioned here. You can create a separate class, call it PageBuilder (or whatever suits your fancy) and move all the building logic into that class. Although I don't know the internals of your code but considering a very generic composite/builder duo here is some ...


2

As designed with roles being a one to one relationship with users, my personal preference would be to ditch the lookup table for roles entirely and go with an enum data type in the users table. Internally in MySql it's going to be stored in a similar way, so it will not take more space. But it will return the string representation in the results making it ...


1

If I'm understanding you correctly, you are providing a list of items with 2 (or more) categories and you want to display them sorted or separated in some way. The options are multiple requests, one for each selected category. Or a single request allowing the client to specify multiple categories. If that's in any way close to the correct understanding, ...


1

It should be absolutely fine. You simply are not assigning the object to any variable and gets disposed of automatically. BUT, I would look at larger scope issues here: If you don't need an object, class(or method I guess in PHP) should be static instead. // Static invocation myClass::myMethod();


0

I have encountered this issue several times and found the following solution at best. Please keep in mind this is my approach and your preference may be different. I always try to keep any form of SQL interaction inside the repository. Therefore the create method should be declared inside the repository. Now the issue with the ID persists. When looking at ...


0

I solved this when I started using service providers with a dependency injection container to implement a module system. Treating modules as service providers for the application is what worked best, whether they are providing new services or providing to data to existing services, like routes. The admin panel has itself become a module that simply ...


2

$this->$data['id'] = $db->insertANewProduct($data); I think is a bad practice, because breaks de Single Responsibility Principle, the product class knows it's been persisted. You are loosing what you win with the repository pattern. If you want to do that another option (less OO) is Active Record. I would choose the option 1 public function ...


11

Comma is a separator between parameters in the parameter list. Comma is part of the parameter lists. Optional parameters are usually specified inside square brackets in documentation like so: [, int $case = CASE_LOWER]. Comma is added to specify that if you were to use the second optional parameter, you must have a comma separator as well. The fact that ...


0

The pattern you are referring to is called Dependency Injection Basically, in most cases, the application calls the library/object for the information it needs, but in some cases, the object needs to first get configured what should it serve the application with. But if a generic object assumes or gets it from a hard-coded location (say config file etc.) ...


0

I see you use php, try to stick to the psr coding styles: psr-1 and psr-2. Look into the decorator pattern. this is a pattern commonly used in these kind of situations. Edit: added a small example You probably have some kind of LineItem class that is used to represent items in your cart. They probably look something like this: interface LineItem { ...


1

A Factory or Factory method is a way of creating objects without having the calling method be aware of the specific class of object that is being created or the dependencies it has. It takes away the complexity of instantiating objects for which (some of) the properties are not known at code-time but can only be known at run-time. Your Entity Translator ...


1

What you probably want to do here is use a repository... This would have your querying methods directly related to your Object and would use and instance of the DB as part of its constructor: $repo = new MovieRepository($db); $movies = $repo->findAll(); And or writing you might have: $repo = new MovieRepository($db); $movie = $repo->findById($id); ...


0

From what I see, you have designed your models in such a way that you can't have a category without posts. You have multiple ways of solving this, I will cover a couple. 1. Utilize Inheritance and have 2 category objects. Have 2 category classes. BaseCategory and CategoryWithPosts inheriting from BaseCategory. Unless you need all posts in your category ...


0

If I have understood your question correctly I cannot see the problem. With posts and categories are stored separately in the database the relationship between them should be stored inside a junction table. Then when you fetch a post by its ID you can lookup the corresponding category inside the junction table. An example: Categories table columns: id ...


4

Starting with the last question, documenting the framework requirements is a must, but having the version check will be useful for those foolish enough not to read the documentation. Having the minimum version as a configurable option doesn't make complete sense, as the minimum version is an intrinsic property of the code itself, not something external to ...


0

The Open/Closed principle allows you to add new methods. You won't be able to alter the interface to existing methods. It may be simpler to have a class that takes a cart and returns/recalculates the totals. Try to avoid using this class to maintain state between calls.


0

I would look at reworking the way you save your data. Profile can exist without picture, while picture can't exist without profile. So, once people are done with their profile, save it. Once they are done with picture, save it separately. Why separate the registration into a number of pages, if you don't save the data independently?


0

Generally, your session name with be PHPSESSID (unless you rename it with session_name - like you've proposed). This really gives no extra benefit in terms of security, here's why; You've mentioned that you're aware sessions can get stolen, which is correct (session hijacking), though the name itself (Alice, Bob) introduces no security measure in this (not ...


0

The pattern that comes in to my mind when reading this is the "Method Object". If you have a big method you transfer it into an object. The state of the method (e.g. arguments) can be part of the object attributes. Changes I see immediately: Do not name the class Algoritm but CombineMotorsAndProducts rename the method combineProductsAndMotors to ...


2

Can I have an object called Algorithm, that is essentially a bundle of methods that loads Products, Motors, combines them, does computations on them, sets up various flags, etc? Or is there a better design? The Strategy Software Design Pattern seems to match the description of your post: https://en.wikipedia.org/wiki/Strategy_pattern This Pattern allows a ...


2

I would improve on OOP part a bit. Provide better naming: You can to change Algorithm to something more specific like RelatedProductsService. Then, expose a method: getrelatedProducts($options) Separate Your responsibilities: You also want to avoid just passing POST along the class into it's children. That is a code smell and creates unnecessary ...


1

Rule of thumb is: If the input comes from anywhere else than from the code itself, the data provided is to be considered 'tainted'. You will have to make sure the value is what you expect and would not mess up your SQL statement (injection). Though I question why you're not using/utilizing PDO and use markers in your statement so these things are handled ...


1

Typically the right place for a file exchange folder would be outside of the webroot where both web applications have access to. So it's anywhere, except where other files reside. Files will generally be FTP'ed or SFTP'ed (FTP over SSH) to the server, or share the same folder on the host if they are served from the same machine (actual machine or VM). Make ...


1

Your application probably have different actions with different access levels, for example an admin will probably be allowed more things than a simple user. So you better build some data-matrix summarizing user(-roles) access-rights and check brefore treating any request if the current logged-in user (or even unlogged Guest if you happen to have such a ...


2

Just use an URL with some query parameters, like <a href="user/5?checksum=12345">Edit</a> Of course the checksum is computed, perhaps using some session cookie, and your server code has something to validate it. So if your user is abusing the system by editing the URL (e.g. replacing user/5 by user/567) the checksum should no more be valid. ...


4

I have encountered this issue several times and have found what I think is the simplest solution. Since your user can edit (personal/account?) information, I assume you have some kind of authentication system up and running. If you use sessions to keep track of recognized clients you can store the user's ID inside it. Then you can fetch the ID from the ...


1

I think as per the PRG pattern if there is a refresh after step2 there won't be an issue as the client(browser) will receive and use the redirect url. The problem happens when the user refreshes before step 2 is completed(before sending the redirect url)!


1

The answer depends on the responsibilities of your wrapping class. If all it does is encapsulate your connection settings for convenience, then simply letting PDOException pass through is preferable. If your wrapper has more responsibilities such as encapsulating certain queries or connection pooling, you may want to develop an exception or two for this ...


0

I'm not very experienced in these terms exactly but I've done a lot of serialization with really complex data types with all kinds of weird dependencies, so here's my 2 cents on managing your POCOs(TLDR; keep them POCO). I have identified some cases where we would actually need to parse the API response data on runtime. Could this be construed as a ...


0

A Node program is at its core a normal program, it isn't even a web-server unless you write code specifically to make it so (though that is pretty easy with the built-in HTTP library). Your program will get the HTTP requests, and your code can respond any way you like, using all the resources otherwise available to the program. If JavaScript was old-school ...


1

Totally depends on your application. I've seen a few commercial solutions: https://prerender.io middleware, which works with Zend Framework2. It's free for under 250 "pages" and works if 7-day caching is acceptable. Also, check out Brombone (sorry not enough rep to post another link...) If you want to go manual, you'll need to do a few things (details ...


0

Unlike all other servers you have mentioned, Node is single threaded, but asynchronous - as some have mentioned here, it schedules callbacks instead of waiting for operation to execute and runs callbacks when the operation is complete, however a number of other operations might have been processed in between with the same thread. Caveats: Although it's a ...


2

You shouldn't catch the PDOException at all. Now, you should have some sort of last resort exception handler that catches all exceptions and logs them. PDOExceptions should be caught there. But you shouldn't catch them anywhere else. In my understanding, PDOException indicates either that something has gone very wrong with the connection to the database, ...


1

I would argue its impossible to handle all exceptions the same way. I think you should let it propagate back to the calling code as it ultimately knows how it should handle the exceptional situation. Just to think of a (maybe weird) example. Imagine you have an blog. An each hour you query the database for new articles. But luck isn't with you and your ...


6

Let's immediately get the Turing-completeness disclaimer out of the way and say any language can probably approximate any runtime feature of any other language. Good? Good. The main difference between the Node.js approach and a Python threaded-server (or a typical Java HTTP server implementation) is that Node.js is single threaded while the latter two are ...


1

Since a data mapper should have a single responsibility, it should only map the results of the query to an object or object(s) and return that back to the caller. In your case we have: Product Motor So, I think one would definately have mappers for those objects defined. I beleive your choices for mapping product and motors are: Composition. ...


4

You are over-using SRP. Let's take a look at mappingConcern's creation-and-initialization version: public function mappingConcern($data) { $parameters = $data; $product = new Product($parameters); return $product; } $parameters is the same as $data, and $product is returned immediately, so it can be translated to: public function ...



Top 50 recent answers are included