Questions tagged [php]
Questions about PHP, a widely-used general-purpose scripting language that is especially suited for Web development.
2,045
questions
0
votes
0
answers
20
views
An IoT client and MVC-based server. Data model with different fields per type?
The problem
Let's assume such scenario:
List of IoT devices (one model?)
Each list item has (among other fields) a type field
Depending on the value in the type field, device may report different ...
1
vote
2
answers
58
views
frontend server obtain data from backend server, or frontend server return page with javascript that obtain data from backend server?
I'm currently learning how to separate frontend server and backend server. However, i'm not sure which approach should i take between the two
Frontend server obtain data from backend server and ...
2
votes
1
answer
105
views
What is the proper pattern for a singleton SETTINGS class load using composer?
I have a class file Settings.php that loads an ini file and assigns the content to a constant for global access and reducing clutter. The class file contains the logic for loading the settings and ...
2
votes
2
answers
216
views
Is it okay to use Dependency injection only because of unit testing?
I have a class that has dependencies that I know are not going to change.
class ConversationFinder
{
public function __construct(
protected Conversation $conversationDbFinder = new ...
1
vote
2
answers
133
views
Best way to protect action links from CSRF
An application has a multifactor login.
The user logs in with its e-mail and password, and then the following screen asks for a one time password received via e-mail or generated by a mobile app.
In ...
0
votes
0
answers
51
views
How to group together common allowed descendants of a vertex in a tree data data structure?
I created an abstract class to represent a vertex in a tree structure. Later, a requirement was introduced where certain types of vertices are not allowed as descendants of certain other vertices. So ...
0
votes
1
answer
253
views
Languages with PHP-like traits?
PHP have what it calls "traits" which despite the name is not like traits in Rust, Scala or other languages.
In many other languages with support for traits, a trait create a is-a relation. ...
2
votes
3
answers
165
views
Organize and maintain a lot of cron jobs
I am working on a fairly large project written in PHP (Yii2) in which we are increasingly using cron for background tasks, such as generating caches, reports, etc.
We have started to move the hourly ...
1
vote
1
answer
191
views
Why does PHP have int and float data types?
I have realized that PHP can treat string variables as numbers (as long as int or float values are stored in the string variables), for example:
<?php
// Declaring two int numbers and one float ...
0
votes
3
answers
807
views
Is it code smell to make an abstract child class override a parent method which only calls its own abstract method
Is it code smell to make an abstract child class implement a method, which overrides a parent method, whose only purpose is to call another abstract method? I want to make sure that anyone who ...
0
votes
1
answer
92
views
How to refactor parallel inheritance tree?
I have a (php) program, which must change yearly. This program calculates tax for every year and there are sometime changes in requirements.
First, the user fills their incomes, expenses, etc.
Then ...
2
votes
2
answers
894
views
Is it good design to have a repository update multiple entities?
I'm building a web application using Laravel. I use the repository pattern as my data layer.
Imagine there's some entity like Product and a product can be assigned to a ProductCategory.
The Product ...
0
votes
1
answer
154
views
Why do the arguments in PHP's array_udiff() comparison function not always stay "in order"?
Recently I was trying to use PHP's array_udiff() function to compare a value in a multi-dimensional array with a plain old string value. I tried something like this:
// E.g. $employees[0]["name&...
0
votes
0
answers
452
views
Best way to check for truthy values in PHP?
In PHP, there are several ways to check whether a value is true, or similar:
true === $v, only works for bool
true == $v || !!$v, works with int and string, but a non empty array can also be ...
2
votes
2
answers
209
views
To maintain SOLID, should data preparation, conversion, and pre-computation for purposes of saving an object, be separate from data persistence layer?
I am facing a common situation where I am saving some values into database from a business object. I am using a relational database and usually I only need to save a few items that are part of the ...
1
vote
3
answers
202
views
Should you reuse a PHP exception's code when wrapping it?
When catching and wrapping an exception in PHP, assuming the new exception doesn't have a meaningful code of its own, should you also use the caught exception's code value? or the default value? In ...
-2
votes
1
answer
127
views
Using Apache and PHP to provide Frontend, possible to use PHP also for backend?
I have a design question to all the programmers out there.
Until now, I wrote most of my software in vb.NET oder C#, especially if I needed a Frontend.
Now I would like to have a Frontend which is ...
-2
votes
2
answers
56
views
How to serve customized content to each user, based on targeting criteria?
In my PHP Laravel application, I want to display special offers to users, depending on the quality of their house(s).
There are 4 Models: User, House, Offer, OfferTargeting.
Each User can have many ...
0
votes
1
answer
465
views
Validation in both controller and my service classes?
I use the Laravel framework.
I've got controllers, like CustomerActivityController, and I've got a service layer with services like CustomerActivityService.
Say I want to create a new customer ...
0
votes
2
answers
169
views
How to refactor code so that a facade class could be decoratable?
I've got a class that is a facade class (encapsulates complex-ish behaviour for reusability). It has a function called manage (the class is called Manager):
function manage()
{
$entityBuilder = '...
0
votes
0
answers
489
views
CQRS, DDD and batch, CRUD-y operations
How to perform basic CRUD operations (especially batch) by playing with DDD and CQRS?
Let's say I have a list of IDs in my controller that need to be removed (soft delete). Currently, I treat this ...
0
votes
2
answers
138
views
Decisions according to environment
I am trying to convince others that the following first code snippet is bad practice and the second snippet is best practice.
Bad practice:
// There is only one implementation of Adapter
public ...
0
votes
2
answers
342
views
Share data between users without a database, php [closed]
How would I create for example a live chat, where you "post" a piece of text and it is displayed to other users in a "chat room", without the need of permanently storing it.
My ...
0
votes
1
answer
248
views
Why doesn't PHP support function overloading (even though it supports type hinting)? [closed]
Why doesn't PHP support function overloading (even though it supports type hinting)? For example why we can't do something like this:
function foo(Student &s)
{
}
function foo(Employee &e)
{
...
-3
votes
1
answer
55
views
Server performance and CMS scalability [closed]
I have been given the job of a colleague who has resigned and I don't know where to start. I hope you can give me some hints:
It is about a CMS made from scratch (Javascript/jQuery/PHP/MySQL) which ...
0
votes
2
answers
637
views
How to concretize a return type when inheritance is used?
I have two repositories:
class RepositoryOne
{
/**
* @param int $id
* @return ModelOne
*/
public function getById($id)
{
// Search and find a ModelOne model in the ...
0
votes
2
answers
267
views
Design patters for handling invoice "payment_status" when split payments are involved
Requirements
I'm building a system with the following requirements.
An Invoice can be paid using multiple Payments (e.g a customer pays the invoice in 2 installments)
A Payment can be allocated to ...
0
votes
1
answer
148
views
Strategy pattern with implemented public method
New to design patterns so my question maybe silly.
I want to use strategy pattern for returning data from a databases in the same format.
Nevertheless it varies the query on the underlying database ...
2
votes
1
answer
681
views
Static validator in DDD value objects
I have a value object to hold a user id number as a string. This number has a unique format throughout my domain. So, it's being validated inside the object during instantiation and an exception is ...
1
vote
2
answers
366
views
QR Code Scanning with location check
I would like to make an order-system with QR-Codes which is online.
How I imagine it to work:
A customer visits a restaurant. There is a QR-Code on his table which takes him to a public webpage where ...
3
votes
4
answers
2k
views
How to store a password so that it can be passed to another site/service which is expecting a plaintext input
I am developing a website and I would like to allow users to use XMPP for live chat. I would like users to have the option use an existing XMPP account if they wish and store their XMPP username and ...
0
votes
2
answers
65
views
Getters and (static) processor vs multiple processor wrappers
I've got a class which stores two data series like so. I need to do some complicated processing on each of these arrays, but for now we'll just get the average of each data series. I could either do ...
1
vote
1
answer
82
views
What is the best approach to use a common variable in multiple method in a request in laravel [closed]
I have a $c variable that is calculated at the beginning of the request. After calculating this several nested methods use it as a part of their job. Is it better that I pass down the $c variable to ...
3
votes
2
answers
162
views
When is it Counterproductive to Separate PHP and HTML
Understanding that it is less load on the server to not have to parse HTML, when does it work the other way, as far as server performance.
The majority of my Web sites are database-driven - often the ...
40
votes
7
answers
10k
views
Why do library developers deliberately break existing code?
Today, I updated ZBateson\MailMimeParser the PHP e-mail parser library from 1.x to 2.x.
Soon enough, my PHP error log started filling up with errors.
Noting where it happened, I found out that it had ...
-1
votes
1
answer
407
views
Adapter pattern for formatting third party API responses
I've been refactoring some of my procedural code to OOP, and I'm wondering if using the adapter pattern is overkill in this case.
Basically, I created a Order class. All class properties are a field ...
0
votes
2
answers
356
views
How to measure the benefit of replacing big parts of the HTML DOM vs doing a new fresh request?
I am looking for a method to measure the time difference between:
the time it takes to load and completely reach TTI for example.com/page-B given that example.com/page-A is loaded.
the time it takes ...
2
votes
4
answers
474
views
Would this violate the Liskov Substitution Principle?
Say I have a set of objects from an "old system" that I want to convert to a newer set of corresponding classes. Each specific class has its own way of being converted.
So I have this:
...
0
votes
1
answer
59
views
"Subtractive" behaviour extension vs overriding a function completely - what are the pros/cons?
Lets imagine some vendor code that we want to extend on our project level
protected function getDefaultFormClasses() {
return [
new FormClassA(),
new FormClassB(),
new ...
0
votes
1
answer
117
views
How to prevent mutual dependencies when implementing database relationships
To explain the problem imagine you have two entities User and Group. The OO implementation has two classes UserModel and GroupModel.
UserModel should have a method getGroups() (returning instances of ...
1
vote
1
answer
447
views
Is it a bad idea to put each class method in its own file? [closed]
We have an existing project that has grown very messy and confusing; our classes are huge and difficult to read.
Our boss wants us to break all of our methods out of the classes and into separate ...
3
votes
3
answers
220
views
Build once or twice: how to synthesize advice from PHP's Composer package manger manual and Continuous Delivery
Authors such as Dave Farley advise us to "only build packages once", so that we can "be sure the thing we’re deploying is the same thing we’ve tested throughout the deployment pipeline&...
3
votes
2
answers
231
views
Visitor pattern applicable ? - Applying rules to a list of registrations
I have a list of Registration's, on which I need to apply a set of rules to each individually.
Rules can be a single Rule, or a sequence of rules, representable by a tree. One rule is a ...
-2
votes
1
answer
437
views
Structure project by inheritance?
The usual approach is to do what libraries do: group your (single file) classes by component where folder = namespace, right? That makes sense for code that is mostly meant to be used by other ...
0
votes
1
answer
45
views
Improve performance of single tenants on shared server
I'm trying to improve the performance of our server. We have 1 server with around 25 tenants, each tenant has it's own application instance and database. There are no performance issues, until, each ...
4
votes
2
answers
341
views
Is there a canonical definition of SPA or is SPA a broadly agreed-on architecture with fuzzy edges? [closed]
Is there a canonical definition of SPA which would exclude the software architecture model described below?
I'm working on an app with a new web-architecture model (new to me, at any rate) which has ...
3
votes
4
answers
979
views
Do all dynamically typed languages not support function overloading?
I have noticed that JavaScript and PHP and Python do not support function overloading.
Do all dynamically typed languages not support function overloading? If the answer is yes, then why is that?
2
votes
3
answers
288
views
Is it best practice to always try to return a minimum number of rows from the database?
First-time poster so please be gentle :)
I have a rather large table in my database (MySQL in case it matters) that holds lat/lng values of certain points of interest around the world (table name: ...
-1
votes
2
answers
394
views
PHP Design Pattern, separation of concerns: should the Controller make a new HTTP request to the API?
I'm having a huge doubt on how to connect the front-end of my application to its back-end.
So my application has a single entry point where all requests go.
The request is dispatched to a controller, ...
2
votes
2
answers
141
views
Use old legacy application code from new application
If you have a big legacy application (big ball of mud), lets call it MudApp.
If you then create a new application to make us of new technology and to make a new good looking frontend because that was ...