Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this questionI have a problem about structures of the complex applications. Because my knowledge background does not come from education, I have always had a problem understanding an application's layers, design patterns, and programing structures. First of all, I can do whatever I want with PHP, because I know common functions and I have experience with it, but I want to create bigger and more complicated applications than I did before, so I always ask questions myself while I am writing code: Is this best place to do this? Is this the best way to do this? Do people use this way to do the same thing?
To answer these questions correctly, I created my own small MVC PHP framework which really looks like Zend Framework. I did this because I want to clarify all parts of the application. I know that there are lots of design crimes in my framework and all my applications, and I think that the main problem is the border between Controller and Model.
I asked lots of questions about this on SO, but still it is not clear for me. Therefore, I will explain what I know and what I do, and please show my mistakes, and correct them, or just explain some information about design patterns, or just explain what my problem is.
What I know
- I know active record pattern. For example, we have user class, we use same class to save data to database, and we use same class as a object. So object is active we can create one then if we change it we can save it with same class ( $user = new user('Oguz'); $user->save();)
- I know factory pattern . We have to classes for one object (User_Factory and User). We use user_factory class to access database for example get user or delete user. And user class is the object it self.
My problem starts when there is a connection between objects (not like many-to-many or belongs-to). For example, we have a video website which has a favorite system. The process of adding favorite consists of these steps (1-check the video with this id, check the user with this id. validating steps). While we are just adding or updating just one object we user other objects too (User factory and开发者_StackOverflow video factory). Generally I can do all this things in controller. But I fell that this is not the best place to do this. Because I call these steps as a process (adding favorite process). So this process should not go in controller because we may want to use same process as an API in another controller-action. So I feel like there should be another place which includes this processes for example process library. I do not even know which programing problem I am talking about.
The connection between object does not just exist in validation step.For example think about search process. when user search a string first we have to create new search row (for latest searches stuff), then we have to search YouTube, if we can not find we have to search other video sites et cetera. So this action is a process search process, I think putting the all logic in controller is a correct way to to this. we use lots of classes and objects so I can not put this process in search objects class.
I think one of the best things you can do is dig in and understand, really understand how things work. In the arena of web development, we are typically isolated from the details of TCP, packets, transports, protocols, etc... But sometimes, it is so isolated, that we forget to learn how it works, and as a result, stick ourselves in a little box.
I've been programming PHP professionally for nearly 10 years. I've never used an MVC framework. I've always separated user interface from application logic from database access.
We don't need a bunch of "patterns". We need true understanding of the problem, and the vision to create an elegant solution, re-using other work as much as possible.
So I guess I am suggesting that you switch your focus from trying to make your application fit into a pattern, and re-consider the logic needed to successfully complete your application. Many times, it is very simple, but we tend to over-complicate it.
Throw off the handcuffs of MVC and patterns... (and then watch me get downvoted for ranting against the status quo).
Good luck with everything. And by the way, you will learn far more about programming in 4 years of doing it, than 4 years of college. Not to say you should or should not go to college, but just be aware that in many cases, you already know more than your professors will about real-world programming.
MVC is a fashion blooming 20 years late. If you feel it pushes you into "wrong" code, it's because that's just what it does. Do things from the first principles. And... The community is not a shrine of divine wisdom. We're just a bunch of loud code monkeys. Use your own brain. And... MVC is a fashion blooming 20 years late. If you feel it pushes you into "wrong" code, it's because that's just what it does. Do things from the first principles. And...
Good luck, and enjoy your endeavors!
精彩评论