0

Well, I know object oriented programming in PHP, and I understand it pretty well (from the basic "person" class with setName, setLastName etc.. methods), but I have no idea, when I would use OOP on an actual dynamic website.
For example, I am currently working on a project (haven't started programming yet) where I will make a website with a MySQL database. So far on similar projects I would just write normal code, but I would prefer to work with objects now.
P.S: I'm still a student.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66
  • 9
    OOP *is* normal code. – Spudley Oct 13 '11 at 14:36
  • 2
    In its current form it's going to be hard to answer this question properly. Perhaps you could tell us a bit more about the project? Have you written an Object-Oriented program before? – fredley Oct 13 '11 at 14:36
  • Why would you prefer to "work with objects"? – Kerrek SB Oct 13 '11 at 14:37
  • I 'll recommend OOP always, it 'll be easy to read, write, understand, extend and reuse the code. – Riz Oct 13 '11 at 14:38
  • I would recommend you to check out the MVC architecture and an OOP PHP Framework like [Yii](http://www.yiiframework.com/doc/api/) to get a feeling for how OOP can be applied to web development. – Sanjo Oct 13 '11 at 14:44

4 Answers4

3

When to use OOP with PHP?

Whenever you like, it's not a way of programming you must apply to when you use PHP. Use the language as a tool.

Related: What are the benefits of OO programming? Will it help me write better code?

I would prefer to work with objects now. P.S: I'm still a student.

If you want to work with objects with your current background, you might want to learn about OOAD as well. That's sort of a subjective comment, it helped me to better understand what object oriented programming is about.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
2

You wrote:

When to use OOP with PHP? .... so far I would just write normal code

The fact is that OOP is normal code.

An object is not much more than a group of functions plus data which are related to each other.

Those functions can just as easily be written as separate functions, and the data can just as easily be global or passed between them all, but putting them together into an object makes it easier to manage. That's really all there is to it.

Consider an object using the built-in PHP DateTime class.

This object consists of a piece of data (the date), and a bunch of functions which allow you to modify it or format it.

You could just as easily have those functions as separate entites (and indeed, PHP does provide them in this form as well), but combining them together into an object makes it much easier to understand.

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

I daresay normal code is object oriented, spaghetti code is "abnormal". There is almost no way you can write a maintainable software without adopting an object oriented approach. Encapsulation, separation of concerns, dependency injection, etc. Very important things for application development and if not impossible surely very unpractical to achieve in PHP without using classes and instantiating objects. So your easy and practical answer is:

Always!

markus
  • 40,136
  • 23
  • 97
  • 142
0

You should use OOP always, or else in few months time you will found yourself trying to modify something and don't know where to start.

For example in your project you can create a class to manage the MySQL connection/queries

if you want to get a more structured project you can also use MVC

SERPRO
  • 10,015
  • 8
  • 46
  • 63