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
    Junior Member
    Join Date
    Jul 2007
    Location
    Orlando Florida
    Posts
    20
    Member #
    15537
    I come here with my tail between my legs. I'll give a trim version of my code below.

    PHP Code:
    <?php
        
    class User{
            var 
    $user_name;
            var 
    $user_pass;
            
            function 
    User($name,$pass){
                
    $this->$user_name=$name;
                
    $this->$user_pass=$pass;
            }
            
            function 
    print_user(){
                echo(
    "user: ".($this->$user_name)."<br />pass: ".($this->$user_pass));
            }
        }    
        
    $user=new User("john smith","1234");
        
    $user->print_user();
    ?>
    When I run this I get output as follows:
    user: 1234
    pass: 1234

    It's like when I set the pass variable in the class file it overwrites the user variable. If my code is scrap, like I'm way off let me know, but I thought it looked correct. I don't have much experience with OOP in PHP, and I'm not sure about the support in 4, although I know it's there, I know it's not as good as in 5. But I'm not able to use 5 at present. Thanks for any help, ideas, or laughs

  2.  

  3. #2
    Member
    Join Date
    Apr 2007
    Posts
    96
    Member #
    15165
    Quote Originally Posted by Prajna
    But I'm not able to use 5 at present. Thanks for any help, ideas, or laughs
    Remove the $ when referencing the variables through this.
    Example ...
    $this->user_name = "blah";
    Note the user_name doesnt have $ in front of it.

  4. #3
    Junior Member
    Join Date
    Jul 2007
    Location
    Orlando Florida
    Posts
    20
    Member #
    15537
    *sigh*, thank you. I feel like I should be slapped. But seriously, the $ is on everything but that. Ok, I will remember.

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Side-note: Don't use OO in php4 - upgrade to php5 as early as you can.

    http://gophp5.org/

  6. #5
    Junior Member
    Join Date
    Jul 2007
    Location
    Orlando Florida
    Posts
    20
    Member #
    15537
    Exactly what I did. This is for a project that is on a friends server. He told me use 4, but, definatly with the size of the project and because our design and development timelines overlap a lot, I will be creating the required class files initially. And OO is usually a better solution anyway. My arcitecture is not going to be that complex from what I have mapped out, so I'm hoping not to have too much of an issue. Hoping is the keyword I guess

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Regardless of what version, OO is a bit of a tagged on afterthought in PHP. Enough better solutions have come up over the past few years that I'm almost ready to start advising people to move away from PHP entirely...

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Quote Originally Posted by Shadowfiend
    Regardless of what version, OO is a bit of a tagged on afterthought in PHP. Enough better solutions have come up over the past few years that I'm almost ready to start advising people to move away from PHP entirely...
    That's not reason enough. PHP's v5 OO initiative is for real. With the large user-base the language has, I believe it'll soon be comparable with Java - I wouldn't even be surprised to see compiled classes come along soon.

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Oh, the initiative is real enough, but the direct modelling after Java smells bad. There are already Java-based interpreters (whether they do any JIT compilation is a different question) for PHP (I forget the name of the one I've seen) and all. Nonetheless, the syntax is still quite clearly an afterthought, what with confusion of dollar signs and the lack thereof, the down operator where there really isn't a manipulable pointer... I suppose that kind of stuff only really feels wrong to those who know many other languages, but there you have it. OO in PHP still makes me feel a little dirty.

    In the end, though, that's probably just my dislike of Java porting over to the overly Java-based PHP5 syntax. As for me, Python and Ruby have been clearly cleaner than PHP in terms of syntax for a long time. And even Python's OO, which I'll be the first to admit also smells like an afterthought sometimes, feels better than PHP's.

    As for `that' not being reason enough -- my reason wasn't that PHP's OO feels tacked on. OO is hardly a requirement for a good language (see Scheme or the functional families like Haskell, for example). My reason for advising people to move away is that there's a whole laundry list of better solutions to the problem PHP was meant to solve, including Python, Ruby, and the .NET and J2EE families (though I'd never advise someone to actually learn J2EE as a beginner...). PHP is still viewed as *the* language for an online beginner, but I'm curious to see whether that'll still be the case a few years from now.

  10. #9
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Quote Originally Posted by Shadowfiend
    OO in PHP still makes me feel a little dirty.
    I know what you're saying... can't argue with it.

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yeah. All the other text in the post was basically me trying to justify that, but at the end that's all it is: a feeling. Much like you can tell when you see well-written, well-structured code, or when you end up having to write a horrible hack, this is just a feeling.


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 09:14 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com