We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Proper DI usage for nested classes

Right now I have a class (Maker) that creates a low level socket (UDP server on localhost:9999)

new maker("udp","localhost",9999,"server")

This maker creates a Raw socket, depending on the data in the Maker constructor.

The Socket Class then do stuff like listen/bind/close/recvfrom etc.

I could make a few getters/setters to fix this. But since I would like to fork new connections it would be nice to implement a proper DI solutio

Currently it feels best to merge the classes to one class. Don't fully grasp the DI concept i see, so I wonder if I'm on the right track.

Can I create a nonshared DI for the socket.

$di->set('socket',function() use ($config) {
 return new Socket($socket_created_in_maker);
 });

I would like Somehow to use that in Maker to create multiple raw sockets and manupulate them directly. But for now i would be happy only to use the functions in the Socket class, with out using a getter in the maker class.

I Guess i made no sence :) but I'm really confused when it comes to DI Im so in to proceduar thinking...

Regards André



8.7k
Accepted
answer

Solved this by looking how they done it in the official phalcon distribution!

All fine for now :)

Regards André