Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP & Object Orientated Code

Options
  • 25-10-2008 6:29pm
    #1
    Closed Accounts Posts: 98 ✭✭


    Hey Guys,

    I'm out of college a year and currently working as a web applications developer using PHP and MySQL. I'm a fairly good PHP programmer but sometimes feel my code is "messy" or not well structured.

    I want to look into OO PHP programming as many companies have this as a must when applying for jobs. I already use some OO to code my web templates where I create a page class and created instances of it for new pages I create.

    But the problem is, apart from this... it seem vary difficult to know when to use OO coding in an application. Like... for a member registration for example, would you create a class member, define some public variables, some functions to insert member, update member, delete member etc...

    Then for a database connection, create a class connection, define variables for the host, password, username, database name etc.. then have a function to run a query etc etc...

    Just wondering how other people look at a problem before they work out how to code in OO style.. or are there any good online resources for it?

    Sorry if this seems a bit rambled. OO is not the easiest thing to explain :o


Comments

  • Registered Users Posts: 2,793 ✭✭✭oeb


    Solarpitch wrote: »
    Hey Guys,

    I'm out of college a year and currently working as a web applications developer using PHP and MySQL. I'm a fairly good PHP programmer but sometimes feel my code is "messy" or not well structured.

    I want to look into OO PHP programming as many companies have this as a must when applying for jobs. I already use some OO to code my web templates where I create a page class and created instances of it for new pages I create.

    But the problem is, apart from this... it seem vary difficult to know when to use OO coding in an application. Like... for a member registration for example, would you create a class member, define some public variables, some functions to insert member, update member, delete member etc...

    Then for a database connection, create a class connection, define variables for the host, password, username, database name etc.. then have a function to run a query etc etc...

    Just wondering how other people look at a problem before they work out how to code in OO style.. or are there any good online resources for it?

    Sorry if this seems a bit rambled. OO is not the easiest thing to explain :o

    Don't worry, the question is pretty common (I am a professional PHP developer)

    One of the main things you will need to look into is programming using the MVC design pattern. (Just google PHP MVC Tutorial) You also probably want to acquaint yourself with the Zend framework.

    That being said, it's only the more up to date studios in Ireland that are using those, most of them still code in PHP4 =)


  • Closed Accounts Posts: 98 ✭✭Solarpitch


    oeb wrote: »
    Don't worry, the question is pretty common (I am a professional PHP developer)

    One of the main things you will need to look into is programming using the MVC design pattern. (Just google PHP MVC Tutorial) You also probably want to acquaint yourself with the Zend framework.

    That being said, it's only the more up to date studios in Ireland that are using those, most of them still code in PHP4 =)

    Really, didnt know that at all. We develop in PHP 5 but dont use any OO design principles at all. Probably because our company is only small and theres only ever 1 or 2 developers working on an application at a time.

    I will defo look into the Zend Framework as suggested and MVC. I suppose, getting to terms with OO design now would be a good idea as more studios will start to venture into OO coding and PHP5.

    Thanks for your reply!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    This might be of help - I found it quite good:

    PHP 5 Power Programming [ http://www.informit.com/content/images/013147149X/downloads/013147149X_book.pdf ]


  • Closed Accounts Posts: 98 ✭✭Solarpitch


    Sound.. thanks for that Webmonkey.


  • Registered Users Posts: 197 ✭✭pauldiv


    Good luck with it Solarpitch. You are brave.

    I just use linear scripts in PHP applications because I find them easy to understand. My knowledge of OOP in Java and C# is very good indeed but I cant get my head around PHP OOP.

    I would like to know why you need the Zend Framework for Object Programming.


  • Advertisement
  • Registered Users Posts: 2,793 ✭✭✭oeb


    pauldiv wrote: »
    I would like to know why you need the Zend Framework for Object Programming.

    You don't.

    Zend framework is one of the (many) OO frameworks out there for PHP. It is just (in my experience) one of the most popular ones on employer wishlists at the moment.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Surely OOP is the same regardless of the language you using. Its about concepts, not about syntex.

    With PHP don't you get all same things you do on others, constructors, inheritance, polymorphism etc etc - I don't understand why you claiming this is difficult in PHP when clearly it isn't.

    Obviously it only worth doing OOP in PHP when you have a relatively medium-large size web application project, but once you have enough concepts, objects it simple enough.


  • Registered Users Posts: 2,793 ✭✭✭oeb


    Webmonkey wrote: »
    Surely OOP is the same regardless of the language you using. Its about concepts, not about syntex.

    With PHP don't you get all same things you do on others, constructors, inheritance, polymorphism etc etc - I don't understand why you claiming this is difficult in PHP when clearly it isn't.

    Obviously it only worth doing OOP in PHP when you have a relatively medium-large size web application project, but once you have enough concepts, objects it simple enough.


    OO is just a design practice. It makes little diffrence to the final application weather it was written using procedural code or OO code. The purpose of it is to make your (the programmer) job easier.

    Part of the reason people have difficulty with PHP and it's OO capabilities I think relates to the way PHP is normally learned. PHP is normally learned in a procedural manner. It can be difficult to adapt. Something like Java or C# forces OO design practices on you.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    That's my point. If you are a good programmer in Java than surely it can't be that difficult with PHP. Starting OO with PHP after doing structural is another thing.


  • Closed Accounts Posts: 98 ✭✭Solarpitch


    I just think the hardest part of writing in OO is when to know to apply the OO concept and create a class. I mean, writing loops, arrays, db connections is the same... but its just the design of the code using classes that I find tricky.

    I just find it hard to look at a project and determine from it how many classes I will need and what for? But if I'm not mistaken, isnt that what the UML diagrams and models are for in System Design?... If so Id say that principle could be applied to PHP come to think of it.. but UML is boring S***.

    At the moment creating a project I would have a single page called db_apps which would contain all the php code and functions for the project, then I just call whatever function I need into the webpage that has to preform that task.
    This procedural method is fine for now but OO will be handy for large projects.

    I can already see the benefit in it when I use OO to design my web templates. Creating a new page is so easy and modification is simple.


  • Advertisement
  • Registered Users Posts: 197 ✭✭pauldiv


    Hi Solarpitch.

    I agree UML looks like boring stuff and I reckon it would take even a bright person a year or so to be really good at it. You dont need to master it to be productive - even a good class diagram is very useful and that is where I started when I wanted to use UML to explain a project I once did.

    If you have a large UML diagram printed off and tacked onto the wall above your work area, it always acts like a reminder of where you came from and where you are trying to get to.

    Sometimes you need to take a sanity check from all the detailed work and your UML blueprint is really useful in this respect. If you have a high level class diagram you always refer to it when you are coding,

    From using this diagram I found that it really makes you think about how software is constructed and how all your objects interact with each other.

    There is a book called "Java - How to Program" by Deitel that has an excellent case study on the use of UML and it also talks about design patterns.

    You dont need to know Java to understand the diagrams.
    You can get a free version of the book online if you know the link.

    Also I just wanted to mention that even though you are not using objects in your php projects you can still have a clean, highly modularised project that is easy to change and manage on a daily basis.

    And thanks for posting because I thought I was the only one who was wondering whether I should take the jump to using OOP for my PHP projects. It's nice to know I am not alone in this respect.


  • Closed Accounts Posts: 98 ✭✭Solarpitch


    Hi Pauldiv,

    Thanks for your post. There's just seems to be so much stuff to learn and know these days when it comes to development. I mean, when I was in college all we ever covered was PHP, Java and C++.

    When I finished and started looking for a job, many places were looking for people who had experience using PHP, Java, JavaScript, Ajax, Ruby on Rails, ASP, .NET etc etc.. and I was like, who the hell knows all this stuff?!?!

    But I think I'll slowly start using OO a bit more and see how I get on. I'll serach google on some PHP class diagram case studies.


  • Closed Accounts Posts: 13 Deserved


    Hello,
    If you want to find a answers just try to do this:

    <?php
    $start = microtime();
    function helloA()
    {
    echo "hello<br />";
    }

    helloA();
    echo microtime() - $start."<br />";


    $start = microtime();

    class hellos {
    public function helloB()
    {
    echo "hello<br />";
    }
    }

    $new = new hellos();
    $new->helloB();
    echo microtime() - $start."<br />";

    $start = microtime();

    class hellod {
    public static function helloC()
    {
    echo "hello<br />";
    }
    }

    hellod::helloC();
    echo microtime() - $start;


    ?>


  • Registered Users Posts: 197 ✭✭pauldiv


    Solarpitch,

    You have PHP and Java from college and those languages alone can keep you in demand for years to come. It sounds like you are more oriented towards backend development and that you will often be required to work with interface designers.

    Interface design can be very involved and all those skills like Javascript, CSS, Ajax, Html et al take years to master. So you need to draw a line somewhere because you cant learn everything. No-one can. Job specifications are a joke sometimes because they ask for something like a dozen of the hottest skills in the market. That is just going for quanity rather than quality and putting some poor guy through the mincer. You want to avoid employers like that becuase they will just ruin your health.

    It's easy to fall into the trap where you constantly feel that the IT game is moving ahead at such a pace that you will never keep up with it all. I've read hundreds of posts from people over the years with this problem and most of them feel overwhelmed by it all from time to time.

    Still, there are things you can do to help yourself in the meantime.
    It takes many years to be a great programmer so you need to be prepared to take years to learn it. It is better to focus on your main language - php - and become great at it rather than worrying about asp and ruby etc. If you focus on PHP you will be more relaxed and you might find time to read about Software Engineering topics like OOP and UML. You will become more talented because after all is said and done talent is nothing but slow patience applied to learning a craft.

    Another good idea is to write a list of skills you like to work at and those you dont like. If you work at stuff you like then you will be more enthusiastic and productive in your work. If you think about this and come up with a plan of what you want to learn over the next few years then you will have more control over what you do in your career.

    So hopefully you now have a little food for thought.


Advertisement