SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Filling in some gaps

    Hello,

    Iíve been reading a lot of books lately and trying to get my head around oop. I have some questions though that i just canít seem to find the answers to, am i just missing the point?

    Firstly people keep saying to me if i learn java, oop will be easier, but surely even in a language like java where you're confined to classes you can write procedural code? This leads me on to my question. Lets say for example i have created some objects for getting and displaying a news page on my site. On the news page i would instantiate my classes (and possibly use a loop like wordpress?). Is this the correct way of doing this? It seems to me that im still writing procedurally on these pages, whereas in java this would be done differently?

    My second question is regarding exceptions. Im not really clear on when to throw rather than echo an error and where should my try/catch blocks be, in the class or on the page instantiating the class? I believe i should always throw an exception from a class error, is this right?

    Finally should SQL be mixed with other class code? If not how should i access the database with other classes or objects?

    Thanks for your time and sorry if i donít make sense

    George

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly people keep saying to me if i learn java, oop will be easier, but surely even in a language like java where you're confined to classes you can write procedural code?
    First and foremost, learn to be far more critical of others advice, especially in software. Second, Object Oriented programming is a paradigm not an absolute concrete requirement. You can accomplish OOP in any imperative language, even those which do not strictly promote objects. Some languages attract more skillful OO developers than others. PHP has a massive share of horrible examples of object usage, so if you want to become an expert on the matter, PHP examples are probalby the wrong choice.

    Java is based on objects and many best practices stem from it and C++. That is about the only argument I can think of in favour of Java over PHP. You can learn good programming principles using PHP, Perl or JavaScript, you just don't have the same tools at your disposal, all the time.

    This leads me on to my question. Lets say for example i have created some objects for getting and displaying a news page on my site. On the news page i would instantiate my classes (and possibly use a loop like wordpress?).
    Is this the correct way of doing this? It seems to me that im still writing procedurally on these pages, whereas in java this would be done differently?
    I think, your thinking to low level to be thinking in objects. Whether you use functions or methods in an object will not improve your design. The oragnization of your methods and the dependencies involved and their management is what separates good OOP from bad OOP or procedural programming.


    My second question is regarding exceptions. Im not really clear on when to throw rather than echo an error and where should my try/catch blocks be, in the class or on the page instantiating the class? I believe i should always throw an exception from a class error, is this right?
    You will start a holy war with this question, but it is in my experience that exceptions are a misnomer. The name is horrible but transcended from other languages like C++ where the name was more appropriate. Exceptions are often stated as handlers for "exceptional" situations, which works well in C++ where a bad pointer might trigger an exception and let your application degrade gracefully, instead of crash outright.

    In PHP it typically takes on a far different meaning, as an alternative control structure intended for robust, simplified error handling. But the errors are quite different. You cannot really crash a program written in PHP (infinite loops aside) as GC and lack of pointer managment, etc really protect the developer from making criticial mistakes.

    If you accept that argument, then it's easy to see why exceptions in PHP are more akin to user generated error handlers and are not really intended to be used in "exceptional" situations but rather they should be used in handling user generated errors, which happen constantly and are expected -- not exceptional (ie: validation of a form).

    All that being said, exceptions are best handled at the core of an application, that way you centralize error handling. You throw exceptions from everywhere within that context and provide enough information in each exception thrown, that the catch handler has enough information to circumvent any errors and let the user try again -- this requires a certain application design to do elegantly without nasty redirects, but IMHO it's a best practice.

    PHP is typically used in a request/response environment, not really event driven like Windows applications, so what that means exactly varies and is difficult to explain without going into details.

    Suffice it to say:

    1. Throw exceptions when the error is user generated and correctable.
    2. Trigger errors when the error requires admin interaction (ie: downed database, invalid file permissions, etc)

    I would personally use exceptions for everything in my inhouse framework, but because some extensions don't throw exceptions and others (like PDO) allow both trigger errors and exceptions I stick with consistency and keep fatal errors as native PHP errors and userland (those which I control) as exceptions, somewhat backwards to their label.

    Finally should SQL be mixed with other class code? If not how should i access the database with other classes or objects?
    No. The idea is to throuroughly separate concerns.

    At a technical level, you should never mingle languages, thus HTML, SQL and PHP should always be kept separate. At the conceptual level, such as Model-View-Controller I have probably 40-50 different layers and sub-layers.

    Cheers,
    Alex
    The only constant in software is change itself


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
  •