SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having trouble understanding OOP with PHP

    Hi guys,

    Having recently learned OOP with client-side Java programming, I now understand alot of the concepts of OOP and how to program using OOP concepts.

    However, now trying to adapt to a serverside language like PHP is confusing me. Since PHP deals with jumping from page to page (as opposed to a client-side language like java), it is hard to adapt classes etc.

    Is there a way to truely use these OOP concepts in PHP or am I missing something?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing you are missing is persistance of objects from page to page. What other problems are you having? OO is just as applicable on the server side as it is on the client.

  3. #3
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are objects passed from one page to another?

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In sessions.

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    often objects will be recreated on each script execution(page).

    but you can store objects in session variables too.
    http://www.php.net/manual/en/languag...ialization.php

  6. #6
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But are sessions always neccessary?

    For example, I have a homepage, and a page with a list of members registered. I need to connect to my database for both of these pages - Do I need to instantiate a new object for both pages? Or can this object be passed?

    Another question I have is, using Eclipse IDE it has a feature that shows you the parameters for my created classes when it detects me using them, is there a similar feature for PHP IDE's?

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a database connection is a resource and so cannot be serialized.

    you would normally just recreate the object and reconnect to the database. persistant database connections are available if you need them.

  8. #8
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone suggest a good book for referencing?

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whydna View Post
    Another question I have is, using Eclipse IDE it has a feature that shows you the parameters for my created classes when it detects me using them, is there a similar feature for PHP IDE's?
    Zend Studio, not free though.
    Saul

  10. #10
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also might look at http://us3.php.net/manual/en/function.serialize.php and http://us3.php.net/manual/en/function.unserialize.php.

    These two functions can be very useful for passing objects between pages in sessions.

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its worth mentioning that you do not need to manualy call serialize() or unserialize() for data put into sessions, even for objects. php does this automatically for you.

  12. #12
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher View Post
    its worth mentioning that you do not need to manualy call serialize() or unserialize() for data put into sessions, even for objects. php does this automatically for you.
    Really! We learn something every day. Thanks clamcrusher.

  13. #13
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess the biggest transition from going from java->php is the fact that in java almost everything is an object.

    Take a game I recently programmed for example:

    The characters, items, map, tiles, doors, window, frames, are all objects.

    Switch over to PHP to a website I am trying to do in OOP - the only objects I have are MySQL.php and Error.php.

  14. #14
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whydna View Post
    I guess the biggest transition from going from java->php is the fact that in java almost everything is an object.

    Take a game I recently programmed for example:

    The characters, items, map, tiles, doors, window, frames, are all objects.

    Switch over to PHP to a website I am trying to do in OOP - the only objects I have are MySQL.php and Error.php.
    I don't see why can't almost everything in php be an object, making your transition easier. Perhaps you should look up php frameworks to help you out. There's no standard framework in php such that you find in java, thus the fact you need to do all yourself may be what you really struggle with.
    Saul

  15. #15
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whydna View Post
    I guess the biggest transition from going from java->php is the fact that in java almost everything is an object.

    Take a game I recently programmed for example:

    The characters, items, map, tiles, doors, window, frames, are all objects.

    Switch over to PHP to a website I am trying to do in OOP - the only objects I have are MySQL.php and Error.php.
    There's no reason that those can't also be objects in PHP. The real trick between going from client-side execution to web-based, server-side execution is dealing with the fact that connections are non-persistent, i.e. you have to rebuild all your objects on each page call. You can store their data (e.g. in sessions), but each page has to load that data up as it reinitializes the objects. It's just a slightly different way of thinking, but OOP is still just as possible in PHP as in Java.
    PHP questions? RTFM
    MySQL questions? RTFM


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
  •