r/PHPhelp • u/SquanchMyZizi • May 12 '26
OOP in PHP
Hello, I started learning OOP a few days ago. I’ve understood the basic concepts quite well: I can easily create classes and individual methods. However, when it comes to creating a Manager class that requires nesting/interacting objects together, I get completely lost. Do you have any tips, useful references, or is it really just a “click” that comes with practice?
Here’s an example:
For the Game and Loan classes, I didn’t have any difficulties, but this is where I get stuck with Library. The code is “correct” because I got help from AI.
In short: I lose track of the types of objects I’m manipulating as soon as multiple classes interact together.
Thanks in advance for your answers.
<?php
declare(strict_types=1);
class Library
{
public function __construct(private array $listeGame = [], private array $listeLoan = [])
{
}
public function ajouterJeu(Game $game): void
{
$this->listeGame[] = $game;
}
public function listerDisponibles(): array
{
$jeuxDispos = [];
foreach ($this->listeGame as $game) {
if ($game->getDisponibilite() === true) {
$jeuxDispos[] = $game;
}
}
return $jeuxDispos;
}
public function listerEmpruntsActifs(): array
{
$empruntsActifs = [];
foreach ($this->listeLoan as $loan) {
if (!$loan->getGame()->getDisponibilite()) {
$empruntsActifs[] = $loan;
}
}
return $empruntsActifs;
}
public function emprunter(Game $game, string $emprunteur): void
{
$this->listeLoan[] = new Loan($game, $emprunteur, new \DateTime("now"));
$game->emprunter();
}
public function retourner(Loan $loan): void
{
$loan->getGame()->retourner();
$this->listeLoan = array_filter($this->listeLoan, function ($l) use ($loan) {
return $l !== $loan;
});
}
}
3
u/cursingcucumber May 12 '26
So what is your question here?
8
u/phpMartian May 12 '26
There’s no way to turn on some magic switch. You keep practicing and reading, that’s how.
6
u/LaGardie May 12 '26
Learn these https://refactoring.guru/design-patterns
1
u/Own-Perspective4821 May 12 '26
The code is correct, because you got help from AI? But you are still having problems and open a thread here?
Eh, what?
4
u/hellocppdotdev May 12 '26
What your looking for is dependency injection and composition over inheritance.
You can model your classes (objects) to represent real world things or concepts, properties for data and methods for behaviour.
If you have a class that requires this object then inject it via the constructor, then use it with $this.
Depending on what you are trying to do this class can then become a orchestrator where you call the behaviour of the injected classes (composition).
That's all really there is too it. Don't go crazy with OOP, just because its complicated doesn't make it good.
2
u/equilni May 12 '26
Not sure what you mean by Manager class. Look at simple examples where one classes uses one or more other classes. This could be basic Controller or Mapper classes. Perhaps this could be Service classes - one example
Pseudo code:
For the library class, from what I can make out, the separate Game & Loan functionality doesn't really belong here as nothing is really working together. It's like you want a "manager" to hold selected Games & Loans, which could be generic or specific to each type
Example:
1
u/BaronOfTheVoid May 12 '26
Honestly, the data structures here are just not very well designed which is why you get easily confused.
You have loans, that have a reference to a game but the same object that holds those loans also holds the games.
Which is why you already need to have the games initialized/ready to use before calling the constructor where you pass loans and games.
You would make your life easier if games would hold the loans each and if you wanted to see all the loans across all the games do a nested loop.
Not a lot is happening here in that short piece of code but still, if your data was a proper tree where responsibilities are clear that would probably help.
1
u/Alternative-Neck-194 May 12 '26
Do I understand correctly, that your problem is that you don’t know which of your objects represents what? Are you using any IDE that suggests the properties of an object?
A good object hides its internal workings from the outside world (the parts that are not important from the perspective of the object’s user), and you communicate with it only through its public elements. If the object is well-designed, then the things you need to see should be self-explanatory.
Unfortunately, I don’t speak your language, so I might be wrong, but I think your Library from this perspective (and only from this) is good. The internal parts are messy so I assume the other two not soo good.
However, if your object has a clear and expressive interface, then in an IDE or a code editor you can immediately see what it is capable of, and you can use it without knowing anything about its internal implementation.
1
u/isoAntti May 12 '26
Don't make it too complicated. It's very difficult to get back to. Use minimum features.
1
u/hay_rich May 13 '26
If your trying to understand OOP itself sometimes I recommend other languages as different languages apply different patterns well differently. For example OOP in Swift feels very different than in PHP. If you want to get good at OOP as it applies to PHP then yes it’s a situation of repeating practice with exercises that benefit from classes
1
u/esonator May 13 '26
The first red flag for me was the sentence where you said the code is “correct” because AI helped you write it.
When I was starting out, our professors made us rewrite example code by hand and test it ourselves. We were annoyed by that, because it felt pointless. “The code is obviously correct, the author wrote it.”
But that was exactly where the learning was hidden.
You can’t really learn something, or understand it deeply, if you let someone else do that process for you. In your case, that “someone else” is AI.
1
u/retro-mehl May 13 '26
OOP is a quite complex concept with many sub-concepts (inheritance, encapsulation, instantiation, etc). You do not need to use all of them at once, they are made to work independently from each other. But you need to understand them.
And to make it even more complex: there are some advanced concepts - like dependency injection - that are built on these basic OOP concepts.
So my advice would be: take a good tutorial and go through all the basic concepts, one after each other, and only go forward if you're sure you completely understand it.
In my experience many developers either need and take the time to understand OOP or even avoid it completely.
1
u/mike_a_oc May 13 '26
I feel that the library class is trying to do too much. It's needing to track all games that exist, all loans that exist, then from that, extrapolate which games are 'available' based on the game itself knowing.
The biggest thing, then, would be to think about "responsibility". What should each class be responsible for? This should be very clearly defined. If you can work that out, the rest will fall into place.
In this case the 'Game' is responsible for being a game. It shouldn't need to know if it is available or not. The Loan is responsible for connecting the game to the person who borrowed it and when.
I think that if it were me I would create a game catalogue class that lists all games, available or not.
When you add a game to the catalogue, it would then add it to the library as being available.
When you loan out the game, you then remove it from the library game list. You could think about the library game list as a 'shelf'. If it is loaned out, it's no longer on the shelf. That would simplify your 'get available games' list because that list would only contain available games. When you check the game back in, you just add it back to the game list.
The library then becomes responsible for knowing which games are available. The game catalogue is responsible for knowing all games that exist. That distinction is subtle but it's important.
You could then remove the getAvailable, checkIn and checkOut methods from Game, because they don't belong there.
1
u/obstreperous_troll May 12 '26
Quick tip regarding something not OOP-related. This pattern comes up a lot:
Consider taking advantage of some of PHP's array_* functions, and replacing all that with:
It's more than just convenient syntax sugar, it's thinking about arrays as single values that you transform with expressions, and you can apply similar thinking to objects and chains of method calls. The switch from handling data imperatively to functionally changes the way you think about programming in general.
0
u/Lumethys May 12 '26
First thing to do would be to only use english
0
u/[deleted] May 12 '26
[deleted] — view deleted comment