Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21
  1. #1
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Ok, I'm making my first large-scale PHP app "from scratch", and I decided to do a lot of it OOP.

    I'm not too handy with PHP OOP, so I need some advice.

    My first business classes are looking something like below... Obviously, I'm using pseudo-encapsulation to expose the "private" properties... but is it worth it?

    Should I just save myself some headache and access the properties directly?

    Anyone have any examples of "good" php oop?

    Thanks! :-)

    PHP Code:
    <?php
    class User {
        var 
    $_id false;
        var 
    $_username "";
        var 
    $_password "";
        var 
    $_firstName "";
        var 
    $_lastName "";
        var 
    $_email "";
        var 
    $_activationCode false;
        var 
    $_receiveMessages 0;
        var 
    $_receiveNotifications 0;
        var 
    $_receiveAdvertisements 0;
        
        var 
    $_address;
        
        function 
    User() {
        }
        
        function 
    getId() {
            return 
    $this->_id;
        }
        function 
    setId($id) {
            
    $this->_id $id;
        }
        function 
    getUsername() {
            return 
    $this->_username;
        }
        function 
    setUsername($username) {
            
    $this->_username $username;
        }
        function 
    getPassword() {
            return 
    $this->_password;
        }
        function 
    setPassword($password) {
            
    $this->_password $password;
        }
        function 
    getFirstName() {
            return 
    $this->_firstName;
        }
        function 
    setFirstName($firstName) {
            
    $this->_firstName $firstName;
        }
        function 
    getLastName() {
            return 
    $this->_lastName;
        }
        function 
    setLastName($lastName) {
            
    $this->_lastName $lastName;
        }
        function 
    getEmail() {
            return 
    $this->_email;
        }
        function 
    setEmail($email) {
            
    $this->_email $email;
        }
        function 
    getActivationCode() {
            return 
    $this->_activationCode;
        }
        function 
    setActivationCode($activationCode) {
            
    $this->_activationCode $activationCode;
        }
        function 
    getReceiveMessages() {
            return 
    $this->_receiveMessages;
        }
        function 
    setReceiveMessages($receiveMessages) {
            
    $this->_receiveMessages is_numeric($receiveMessages) ? $receiveMessages 0;
        }
        function 
    getReceiveNotifications() {
            return 
    $this->_receiveNotifications;
        }
        function 
    setReceiveNotifications($receiveNotifications) {
            
    $this->_receiveNotifications is_numeric($receiveNotifications) ? $receiveNotifications 0;
        }
        function 
    getReceiveAdvertisements() {
            return 
    $this->_receiveAdvertisements;
        }
        function 
    setReceiveAdvertisements($receiveAdvertisements) {
            
    $this->_receiveAdvertisements is_numeric($receiveAdvertisements) ? $receiveAdvertisements 0;
        }
    }
    ?>

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    I think it's perfectly okay to do it that way, though I believe it's generally less common in the PHP world. PEAR seems to be coded mostly in OO, so I suppose it's a perfectly good idea to look at how it does things.

    Also, if you're running PHP5, there's support for visibility modifiers in the same vein as Java's.

  4. #3
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,657
    Member #
    1234
    Liked
    139 times
    damnit, I never did get the $this crap. something like an internal reference to the object that's been called, and the -> is talking about the var instanced to that specific object?
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  5. #4
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    I'm sure you know encapsulation is good, but I'm sure you know just as well that it's super-annoying to make all those accessors.

    [minicode]$this[/minicode] is just like [minicode]this[/minicode] in any other language: it's a keyword that, when used in a class, refers to the current instanatiation of that class. In PHP, it's required when accessing anything in the class because PHP is too stupid to understand it otherwise.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  6. #5
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,657
    Member #
    1234
    Liked
    139 times
    I understand encapsulation / objects, which in my mind is just a fancy way of making a Pascal class (sort of), just can't fit it into my head in terms of how I'd code with it. Brick wall when it comes to that.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Well... That's a hard one to explain in a short post :-)

    [minicode]$this[/minicode] in PHP explicitly scopes the variable as belonging to the class. PHP doesn't seem to have a very good understanding of variable scope; or, rather, it always requires explicit scoping operators in order to get to variables that are in a scope other than the current one. Unfortunately, in a class method, the `current scope' isn't the class, it's the function; so, in order to access the member variables, you have to add the explicit scoping with [minicode]$this[/minicode]. This is much like the `global' operator that tells a function to look in the global scope for a variable.

    This is in large part a side-effect of the fact that PHP creates a variable automagically when you first use it. Instead of searching first for uses of the variable name in other scopes, then, if it doesn't find any, creating the variable in the current one, PHP opts for creating the varaible in the current scope unless it is explicitly told to look for it in a different one.

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Many a time you'll wonder what the hell is going on when all you're missing is a [minicode]$this[/minicode]. Hence why I hate PHP OO.

    And did you know, on another matter, that true == false in PHP? After all, null == true and null == false, so by the associative property, true == false.

    There's also a [minicode]$that[/minicode] keyword, but I forgot what it's used for...I think the object that invoked your method or something like that.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    null is true *and* false?? That's just... Weird... I didn't know that.

  10. #9
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,657
    Member #
    1234
    Liked
    139 times
    true and false aren't values, just opposites.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  11. #10
    Senior Member mixu's Avatar
    Join Date
    Aug 2003
    Posts
    217
    Member #
    2815
    Transio, could you post some information on how (conceptually) you are modelling your classes? And perhaps some experiences?? I'm fascinated by the idea of writing OO PHP in my next big project -- but I'm still too deep in the Perl world and in the business of maintaining legacy PHP code that I'm having a hard time making the conceptual jump.

    I mean, when it comes to regular application development OO is great but I find that the HTTP request-response model is quite challenging to accommodate conceptually into OO classes.

    Filburt1: I bet you have a better reason than just the syntax for not using OO PHP? Hate is such a strong word for something which is just a tool for software development.


Page 1 of 3 1 2 3 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:17 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com