r/PHPhelp • u/MorningStarIshmael • Feb 16 '26
When to choose a function over a class, and vice versa?
I'm sure this is a common question, but I'd like to know what the criteria are for choosing a function or class to improve or add a feature.
I ask because in the Laracasts beginner course, there's a lesson where the host, Jeffrey, creates a Validator class with a string method to ensure a string's length is within a specified range. He also said that, later in the course, he would come back to that class to add a method that confirms whether a string is an email address.
The question is, considering that these are fairly simple methods, what's the benefit of creating a class over a function? In this particular case, it seems to make sense, since you have various functions that belong to a specific theme (validating strings), so, is that a criterion?
What are some other criteria for whether to orient the application toward objects or functions?
5
u/Jutboy Feb 17 '26
The framework you are using will largely decide this question
5
u/equilni Feb 17 '26 edited Feb 17 '26
To add a feature, a class.
To improve a feature, you need to be more specific here as this can be done in either depending on what it is.
Typically once code is written, you try not to change it too much because you or others may depend on that code (otherwise document it well).
Classes are good because, at basics, you can do thing like Extending/inheritance and Interfaces/Contracts a class needs to adhere to.
Classes can also be autoloaded, where functions cannot.
It is a design choice and consider adding too much would become a maintenance and testing nightmare. He finally adds 3 methods to the class, but think about all of these (Laravel Validation rules) in one class?
In a bigger scenario, an Interface could be better because String Length and Email address are different concerns, so you could do something like:
See how each class implements the Interface contract and defines it's own method body?
This cannot be done with functions.
EDIT added github link
4
u/jmp_ones Feb 17 '26
Autoloading. You will need to call
includeorrequireto load the file for thefoo_bar()method. You won't need to forFoo::bar()ornew Foo()->bar()method call when autoloading is enabled and you follow a naming convention recognized by the autoloader (cf. PSR-4, on which I was the lead.)3
u/xreddawgx Feb 17 '26
Class when you want to create an often use object with multiple attributes.
Function to do complicated repetitive tasks.
3
u/Mastodont_XXX Feb 17 '26 edited Feb 17 '26
Classes are a way to model concepts that have state and behavior (properties and methods for working with properties). So it could usually represent a thing (User, Order, File) or conceptual role (Validator, Parser, Repository). Unfortunately, many programmers use classes for everything.
Personally, I am always in favor of functions in templates. For example, it is much easier to use
than
Advice from AI: Use a function when you're modeling a behavior (do something). Use a class when you're modeling a thing (an entity with state and behavior).
3
u/martinbean Feb 17 '26
To me, it’s just taking a moment and thinking about the words themselves.
A single function performs a function: you give it input, and it has output. If you have a number of relation functions, then you can group them in a class as that: a class of functions. If you stick to this rule, then it should help you ensure classes contain functions (methods) grouped around one concept, and you’re only creating classes as and when you need to.
Another litmus test for me is, create a class when you’re discussing a noun (things) in your application. So when you’re talking about how something works (“X can do Y”) the “X” is a noun and usually something that can be represented by a class with properties describing that thing, and the Y is a process that can be encapsulated in a function/class method.
2
u/obstreperous_troll Feb 17 '26
If it's static methods vs standalone functions, then it's mostly a matter of personal taste. Classes are autoloaded, so you don't have to require() them or remember to add them to your autoload files. You can get some module-like behavior out of class names by way of
$myClass::someMethod()but for that it's usually best to stick to instances.Popular opinion these days is to prefer classes over standalone functions. You'll definitely want a non-static class if it's something like a
Validatorthat you're going to inject as a dependency in the framework.2
u/secretprocess Feb 17 '26
If you're using Laravel you want pretty much everything to be a class because that's how Laravel organizes things and you want your things to fit neatly into the framework.
In a more generic sense, a function should really just handle one very specific task. Once you find yourself adding different modes or lots of arguments to a function you're probably better off with a class. And the more you do it the more you realize that's most of the time.
2
u/mnemonikerific Feb 17 '26
For static functions, the answer is code reliability.
I find using classes improves traceability and readability.
Then there is a hidden future benefit. In case you need to have a static class level private state, you are now covered, and don’t need to refactor the code.
2
u/g105b Feb 17 '26
I highly recommend Robert Martin's book Clean Code that explains this well.
A function should perform one unit of functionality that can be named. When you have lots of units of similar or adjacent functionality, you can organise them all inside a class.
His book provides examples with lots of rules. It's a dry read, but changed my daily development forever.
2
u/lemon_tea_lady Feb 17 '26
For simple, stateless operations like string validation, functions are often cleaner. You’d typically reach for a class when you need shared state across methods, when you’re building toward composability, or when you need polymorphism.
I generally ask myself: does this structure match the problem I’m solving?
Start with the simplest thing that works, then add structure when the complexity starts to require it.
2
u/ahgreen3 Feb 17 '26
Here's a bit of a different take: ALWAYS make a class when doing something new. If you are doing something related to something currently existing, consider adding a method verse a new class.
NEVER* create a standalone function. Since PHP 7 (really 5.2 if I remember correctly), the language is fundamentally focused on an Object-Oriented Programming approach. With autoloading, namespacing, etc available to classes, you should avoid standalone functions as much as possible due to the long-term pitfalls (name collisions, intentional loading, isolation of responsibility, etc).
I specify a standalone function (ie a named one in the global scope) rather than any function because anonymous (closures) and scoped functions can be tremendously valuable and should be defined/used as appropriate.
* There are 2 caveats:
app()andconfig()in Laravel.)2
u/AshleyJSheridan Feb 17 '26
I assume you're talking about standalone functions versus classes with methods (which also use the
functionkeyword).Classes are great for grouping functionality together. For example, a helper class that deals with strings can have methods for altering them, checking their contents, etc.
This keeps your code a little tidier, and also lets you have methods of the same name across different classes, something you can't really do with standalone functions (because they are all part of what's known as the global space).
Classes can also maintain their own state. Instead of passing around objects to each method (like you would do with functional programming), the class instance can handle itself by itself. So, if you had a
Drawingclass, each instance might maintain its own GD object, and you can have various methods to draw onto it, likedrawRect,drawCircle,writeText, etc.Lastly, classes can inherit from other classes. Basically, you would have a pretty basic base class, and each new class that inherits that would be more specific. You can chain these as much as you want. A classic example of this is vehicles. Imagine this list like a series of class that inherit from the one just above them:
The topmost
Vehicleclass will be too basic and generic to use directly, but it's a good base for putting in everything that's common to all the vehicles. So, in there you might have anOwnerand some methods for setting and getting thatOwner. To make the code more tidy and ensure that nobody accidentally uses this class directly, you could make itabstract.Next, your
Carclass would inherit fromVehicleand add things specific to cars:Then, your more specific car types can add things that apply only to that type, not to the generic
Car, like number of seats or doors, etc.Classes in any language get really powerful when you add inheritance into the mix. They can help keep things tidy and compartmentalised, and reduce duplicate code blocks.
That said, functional programming, even within PHP, is still a useful tool, and the two things can work side by side. In-fact, a lot of the internal functionality of PHP is functional, especially the older stuff, and methods that accept callbacks tend to be easier to work with if you're using simple functions rather than class methods (although you can use either).
1
u/oshjosh26 Feb 26 '26
Short answer: It's personal preference.
PHP is cool because it's what's called a multi-paradigm language. That means you can program in many different styles. Organizing your code by plain functions vs organizing your code with classes and objects are only two different styles of writing reusable pieces of code.
There is much debate about different paradigms, but none is better than the other. Programmers often develop a taste for one or the other and it's entirely up to them.
Try them both and see what you like best. It's up to you.
1
u/Vroomped Feb 17 '26
What your asking is theology, religion, inherited from your teachers, and a matter of opinion. I recommend just pick a group and stick to it. Your code will be legible and if critiqued "Its a widely recognized standard with reason" will be a better stance than vibes or ease of use.
I use Fig https://www.php-fig.org/psr/