Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts

    the result of the object?

    PHP Code:
    <?php
    $object 
    = new User;
    print_r ($object);
    class 
    User
    {
    public 
    $name$password;
    function 
    save_user()
    {
    echo 
    "Save User code goes here";
    }
    }
    ?>
    i am a novice of php,i can't understand the above code's result.who can explain it for me? the more details of it .the better. any help would be appreciated!

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That is not procedural (step-by-step) code. That is a class definition, or object-oriented code (OOP).

    Basically, the author has created an object (User) with properties ($name,$password) and methods ( save_user() ). It's kind of like creating a new variable type, like int or string.

    For more, check this tutorial out: http://www.killerphp.com/tutorials/o...cts-page-4.php

    Dan
    Last edited by whizard; 08-25-2009 at 05:31 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks! but in class User ,there is no return value? but how when print_r($object) the result is "User Object ( [name] => [password] => ) "?how this come from?
    thank you!

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Well in the code, the properties have never been set.

    Usually you set all the important properties with a constructor, or the method called when the object is created.
    http://us2.php.net/manual/en/languag...onstructor.php

    Dan
    Last edited by whizard; 08-25-2009 at 05:53 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    how is the above code execution flow ?is it in sequence? or first execute the class.
    thank you

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    1. Class definition loaded into memory. This happens at inclusion time if I'm not mistaken, and since its embedded in the same script it will occur when the page is loaded, but before parsing occurs.
    2. new constructs a new instance of an object blocking aside space for zval and returning an object reference (which can later be used to lookup the zval*)
    3. print_r displays hashmap association between key => values.

    Methinks thats about how it would play out. I don't think thats what you're looking for though.
    Using an object is still done in a rather procedural way. You call them in the order you want it to process. Writing the class is different though, each property and method is independant of each other and can be placed at any location within the class definition. Since the entire class is loaded into memory at runtime, Zend engine would prototype the necessary procedural functions from the class providing you with access to any other method whether it has been declared before or after the method in question.
    Does that answer you're question?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Basically, this part (the class definition):
    Code:
    class User
    {
    public $name, $password;
    function save_user()
    {
    echo "Save User code goes here";
    }
    }
    is loaded into memory first.

    Then, this part:
    Code:
    $object = new User;
    print_r ($object);
    is executed procedurally.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by whizard View Post
    Basically, this part (the class definition):
    Code:
    class User
    {
    public $name, $password;
    function save_user()
    {
    echo "Save User code goes here";
    }
    }
    is loaded into memory first.

    Then, this part:
    Code:
    $object = new User;
    print_r ($object);
    is executed procedurally.

    Dan
    o.O
    I didn't think it could be said any simpler than the way I said it
    Lol
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    I think that once you've reached a high level of mastery and understanding of a concept, it can be hard to describe it in a way that makes sense to a beginner. Your post was an excellent description, if you understand inclusion time and hashmaps and zvals. I know that I struggled for years with classes and OOP because I had no clue what many friendly people were talking about when they tried to explain it.

    I will never compete with many in this forum in the sheer volume of knowledge and helpfulness, but I seem to be able to bridge the gap between experts and beginners, perhaps because it is in that void I stand.

    The great thing about this forum is that there is excellent team work and community, and together, we can solve any problem!

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    both of you guys' replies are terrific! thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •