SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php with classes or without classes?

    Php with classes is complex, without classes its easy. Whether to use classes or not?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That is a matter of opinion concerning complexity. Management, maintenance, reuse-ability in my opinion is far more complex with procedural.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by logic_earth View Post
    That is a matter of opinion concerning complexity. Management, maintenance, reuse-ability in my opinion is far more complex with procedural.
    so you suggest without classes?

  4. #4
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Offcourse with classes. PHP without classes is pretty stupid. It is much more powerfull with.

  5. #5
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TwoR View Post
    Offcourse with classes. PHP without classes is pretty stupid. It is much more powerfull with.
    I've heard that large php applications like vbulletin i.e. the one which we are using at sitepoint, are simple php without classes.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It very much depends on the situation/project and whether you want to reinvent the wheel every time.

    Certain things lend themselves to procedural coding, others to OOP (classes) coding. (Pretty much all situations can be made to work using either approach, but whether you SHOULD is another matter).

    Things like applications using a database backend ... if the database is ALWAYS going to be, say, MySQL and it's a bespoke applciation then you can do it procedural. However if you are creating an application interface but can't guarantee what the backend database will always be, then OOP is the clean, elegant approach ... the application is written to provide the features/functions needed, and the only customisation is the class you call dependant on the database type.

    When the application is ported to another instance then all that's done is to call the appropriate database class type - doing it procedurally would be a nightmare; the application would likely need rewritten.

    My 2 anyway.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    It very much depends on the situation/project and whether you want to reinvent the wheel every time.

    Certain things lend themselves to procedural coding, others to OOP (classes) coding. (Pretty much all situations can be made to work using either approach, but whether you SHOULD is another matter).

    Things like applications using a database backend ... if the database is ALWAYS going to be, say, MySQL and it's a bespoke applciation then you can do it procedural. However if you are creating an application interface but can't guarantee what the backend database will always be, then OOP is the clean, elegant approach ... the application is written to provide the features/functions needed, and the only customisation is the class you call dependant on the database type.

    When the application is ported to another instance then all that's done is to call the appropriate database class type - doing it procedurally would be a nightmare; the application would likely need rewritten.

    My 2 anyway.
    So you mean to say that I may not use classes for small projects?

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No. I didn't say that at all. I said you should choose the method that best suits ...

    a) Your capabilities

    b) The type of project - It doesn't have to be a large project for OOP to be preferred: a small project that will have many uses would be a perfect candidate for OOP/classes. In fact, a small project done in OOP might be a good way to get to learn and understand OOP.
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    I've heard that large php applications like vbulletin i.e. the one which we are using at sitepoint, are simple php without classes.
    vB has at least 40 classes in it. It's still mostly procedural but it's being updated.
    Adrian Schneider - Web Developer

  10. #10
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SirAdrian View Post
    vB has at least 40 classes in it. It's still mostly procedural but it's being updated.
    Is it good to use a mix of classes and procedural programming?

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That shouldn't really be a question that you should ask. You need to use what works best for the situation. Don't use classes just for the sake of using classes.

    Classes provide a lot of advantages in some situations over procedural code. For example, if you have five different types of users and each one has a different block of code for an "register" function, what would you do in a procedural situation? If you have code to call this register function, that code would have to be aware of the five different type of users and call the right procedural function. What if you want to add more types of users? You would have to update that registration code to know of these other types. However, if you were doing this with classes, the code would be much more modular because if you want to call the register function for a type of user, you could just refer to the instance's respective method.

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    so you suggest without classes?
    No...I do not.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are working on a web application, or a large website, then yes, you will want to use classes so you can re-use code. It will save you a ton of time in the long run, but it is more difficult to grasp and even to write. For smaller sites/apps, it will be more work and more code, but as the scope of the project grows, usually so do the benefits. I've seen old code of mine where it's been cut down to about 20-30% by updating it. It was several times harder to write, but the code comes out much easier to read, more concise and easier to maintain.

    You can't just jump into it though... do it your own way, and experiment as you learn how to write better code.

    re: vBulletin -- They moving to a fully class-based system in the future, but are moving with baby steps (3.0x had a few classes, 3.5-3.8 have many, etc.)
    Adrian Schneider - Web Developer

  14. #14
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For me the only benefit PHP OOP offers is the best way to organize and manage code. I just don't see any other practical usages as objects are loaded and unloaded per request. So in my opinion procedural specific functions (to be used in the current script) type scripting is most times more resource friendly. As for vB, sure it has many classes, but they all use what I call the NO NO NEVER IN OOP type importing of variables. In my opinion if PHP could load objects into it name space (module specific), then the full benefits of OOP could be realized.

  15. #15
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a lot more too PHP OOP than just a bunch of classes and objects. Aspects such as very precise control and well organized reusable code tilt the balance heavily in favor of oop than procedural code. That being said one can not rule out procedural code completely. One has to take efficiency in to account when coding. There is "using a truck to move heavy stuff" and then there is "using a truck to go to the around the corner" while it would have been a lot easier to walk back and forth. So in short OOP is not one stop solution for everything.

  16. #16
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I will use the same code two or more times, I think about it for a bit to see if it's worth creating a class I can instantiate, otherwise I do it procedurally.

    It doesn't always work out, but for the most part I don't like to create classes I will not instantiate two or more times, it just feels like a waste.

  17. #17
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $man = getClass('person/man');
    $man->setName('john');
    $man->setHeight(60);
    $man->setWeight('bad data'); // This will throw an error. You only have to verify this data in one area, which is why a class will make your life so much easier.
    $man->save();

    Thats the advantage of classes. You only have to check data in one area.

  18. #18
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Certainly classes designed, not for the sake of doing class but for more structurally sound, is far better than any procedural. When you design a class, you think ahead. Not only does it add structure to your code, but it will lend itself usable in future projects. So the initial time spend designing the class(es) is rewarded many times over on the long run.

    One last advice is though, study OOP more thoroughly before you attempt to put together any class. A class with many functions in it is not clever. That is procedural in different name.
    ------------------

  19. #19
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know OOPs very well still I prefer using procedural programming because of simplicity. I find classes complex.

  20. #20
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    I've heard that large php applications like vbulletin i.e. the one which we are using at sitepoint, are simple php without classes.
    There are a number of widely used PHP based applications, that began life at a time when OOP in PHP was either non existent, or not up to the job. This is no longer the case, but the core of those applications are much too complex to take the idea of re-writing them in OOP lightly. There no doubt are a number of people on the vbulletin team who are tasked with maintaining and upgrading the code, that wish that it was OOP based.

    I personally stopped writing ANY core functionality for any given project more than a year ago. If during work on a project, I run across a situation where I need core functionality that my class library does not have, I develop that functionality for the library NOT for the project. The only project specific code I ever do at this point is implementation. That cuts down on development time, and code changes considerably, not to mention errors and debugging time.

    I detest writing the same code over and over again for different projects. If you make your core functionality generic and reusable through OOP techniques, you never have to do so.
    Visit my blog
    PHP && Life
    for technology articles and musings.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •