...

View Full Version : the result of the object?



runeveryday
08-25-2009, 04:29 AM
<?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!

whizard
08-25-2009, 05:28 AM
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/object-oriented-php/php-objects-page-4.php

Dan

runeveryday
08-25-2009, 05:46 AM
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!

whizard
08-25-2009, 05:48 AM
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/language.oop.constructor.php

Dan

runeveryday
08-25-2009, 10:30 AM
how is the above code execution flow ?is it in sequence? or first execute the class.
thank you

Fou-Lu
08-25-2009, 10:40 AM
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?

whizard
08-25-2009, 03:16 PM
Basically, this part (the class definition):


class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}
is loaded into memory first.

Then, this part:

$object = new User;
print_r ($object);
is executed procedurally.

Dan

Fou-Lu
08-25-2009, 04:45 PM
Basically, this part (the class definition):


class User
{
public $name, $password;
function save_user()
{
echo "Save User code goes here";
}
}
is loaded into memory first.

Then, this part:

$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 :D
Lol

whizard
08-25-2009, 05:42 PM
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

runeveryday
08-27-2009, 08:16 AM
both of you guys' replies are terrific! thank you very much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum