Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP - how much should I develop classes? [closed]

Tags:

oop

php

class

I've just started using classes, leaving procedural programming in the past. But there are some doubts I have while I'm moving forward.

1 - As I wrote in the title, how much should I develop classes?

What I've got so far is, for the register/login sections of a site, this developed classes:

  • data access layer (called from almost every class)
  • register (validates form and sends email with activation code)
  • user login
  • activation (checks code / sends new email with new code)
  • pass recovery (checks recovery code / sends new email with new code / processes new password form)
  • pass encryption (called from register and pass recovery and uses bcrypt)

For example: - both in register, activation and recovery, I send emails.. should I create a class "mailing"? - in register, login, activation, recovery I use tokens and captcha.. same question..

Do you understand what I'm asking?

2- On other hand, I'll create a session after user logs in. I'll have to retrieve user information such as: Username, Id, Credits, history of operations, ads (created by the user and other ones stored but created by others)..

I can't figure out the way to start.. I should create classes for example: - Personal info (updates) - Operations - Credits - Ads (a class for those he creates and other one for those he'd like to store)

I'm really confused and I haven't found clear information about how to organize each stage/section..

like image 629
buu Avatar asked Apr 27 '26 17:04

buu


1 Answers

It depends on the way you want to do the whole website. For big webs, it turns necessary to use a MVC framework (Zend, Symfony, Yii, CakePHP, Silex, each one with pros and cons, as always depends on your needs).

In those cases, anything related to database goes into a model, which leaves the DBAL to the MVC engine, letting you use database objects as classes.

Anything that is in the representation layer (that means, anything that shows information in one or another way) is a view, which is nothing more, nothing less, than an HTML file with some echo calls to parameters, or template variables, in case you want to use a template system like Smarty.

Anything related to how things are done in your system is a Controller's action, which fits in a Controller, another class, also abstracting the way to do things.

And in the cases you want to validate inputs, treat sessions, etc, it actually depends on the framework you use. In my case, I'm used to Yii, which lets you create something called Components (again classes) to manage stuff that will definitely be reused in other projects.

Each class (Component, Model, Controller, etc) extends a different parent class, so, despite all of them are classes, each one has special methods that make them more practical to do some tasks.

As you see, almost everything can be a class, and it depends on your needs, your framework choices, and your style, on how to code them.

like image 156
Sergi Juanola Avatar answered Apr 30 '26 06:04

Sergi Juanola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!