Class instatiation, the "new" keyword, object handling

The usual way of creating objects in classes with constructors:

$object_handle = new class('string','empty');

Lets say that the constructor looks like this:

function __construct($passed_value, $alternative_value)
		if(isset($_POST[$passed_value]) AND $_POST[$passed_value]!='')
			$this->property = $_POST[$passed_value];
			$this->property = $alternative_value;

Obviously, I would need $object_handle to make use of the methods in the class. However, what if I only wanted to create the object, like this:

new class('string','empty');

Isn’t there any internal handles that can be fetched after the instantiation? I can’t find any php functions that provide this functionality, neither is (as far as I can see) object handling described in such detail in the reference. I can’t find anywhere that this is explicitly stated as impossible, so my gut feeling tells me this should be be possible in one way or another.

In other words, what good are OOP if there is no way of getting the objects in a class without knowing their names in beforehand? If OOP really should prove helpful, then this must be it.

Comments appreciated…

I don’t quite know what you’re trying to achieve. What do you mean by “getting the objects in a class”?

I have no idea of what you saying.
Here are some points for you:

  1. Constructor function does not create an object. It’s just the first method that is automatically called after the object is created. This is important to understand.

  2. Constructor is not required, it’s optional, you can create an object that does not have a constructor.

  3. You need to provide more information and tell us what exactly you need to achieve, then someone will help you.

hello and i wondered the same thing for awhile but my conclusion is php is not built as lets say smalltalk where everything is an object. Think of it this when you writting the class and accessing the different methods and properties you use $this-> this is your object handle this is only used in scope for that particular class so when you need to extend the scope you must overwrite $this-> with your new object handle lets say it is $foo ($foo = new class) so now foo takes the place of $this-> and that is why it is almost impossible to just use the object creation key with out a variable you need to overwrite $this.

Without assigning the newly created object to something, like a variable it is instantly DESTROYED. Because there are no references for it to cling to thus the object ceases to exists the moment it is created. While you can create an object like this without assigning it (ex: $var = new object;) it will not remain long enough to do anything with it.

Seems you cannot have a class called class it’s a reserved word, just saying.

  1. When you have thousands of lines of code, how do keep track of the objects that is currently instantiated in a given class? The closest thing I get is using is_a(), but then I have to know the object name. The point is that I don’t know all the objects.

Fair enough, provided that you are correct. It seems like a feasible explanation, although it is difficult to be at rest with it. Maybe my insight widens later.

Preliminary conclusion: must assign objects to variables at all times, or else they disappear.

As mentioned above, you assign objects to variables so that you can access them later, i.e.

class Person
    public $firstName;
    public $lastName;

    public function getFullName()
        return $this->firstName.' '.$this->lastName;

// calling code

$person1 = new Person;
$person1->firstName = 'john';
$person1->lastName = 'doe';

$person2 = new Person;
$person2->firstName = 'bob';
$person2->lastName = 'stevens';

echo $person1->getFullName();
echo $person2->getFullName();

I am not exactly sure of what is being asked, but take aamonkey’s code above, and using Xdebug and having xdebug.collect_params turn on in my ini - I get a trace file in /tmp with this:

TRACE START [2011-05-11 18:13:50]
    0.9392      55560   -> {main}() C:\\var\\www\\html\\code\	race_test.php:0
    0.9394      56552     -> Person->getFullName() C:\\var\\www\\html\\code\	race_test.php:24
    0.9394      56608     -> Person->getFullName() C:\\var\\www\\html\\code\	race_test.php:25
    0.9516      15968
TRACE END   [2011-05-11 18:13:51]

So its like a trace file but without all the timings, memory usage and so on.

OK, so let’s say that you want your script to take multiple $personX from some kind of input then 2 problems arise:

  1. How do you create variables ($personX) programmatically (in the example above you have explicitly written $person1, $person2 etc., but you can’t do this for an unknown quantity).
  2. How do you access them later

You could just store them in an array.

$people = array();

foreach($input as $details){
  $person = new Person;
  array_push($people, $person);

However, this is greatly dependant on what you’re trying to achieve. :slight_smile:

OK, so let’s say that you want your script to take multiple $personX from some kind of input then 2 problems arise:

How do you create variables ($personX) programmatically (in the example above you have explicitly written $person1, $person2 etc., but you can't do this for an unknown quantity).
How do you access them later

well to make variables that are really unknown you can use parse_str() this parses a string into a variable. and well to call it well i would check if the variable exists then return it from a function dirty but works…

Ok, now we’re talking…! Problem solved!

And: You’re right, it’s dirty, but it works! :cool:


Not as neat as Anthony’s suggestion, and really not very readable at all (variable variables suck), but this would work too:

foreach($input as $key => $details){
  $var_name = 'person' . $key;
  $$var_name = new Person; // note the double $-sign here

If you have, say, 5 entries in $input, this would leave you with 5 objects containing one person each:

More info:

I’m not quite understanding what the point of creating a ton of named variables is. Usually if you need an array of objects (as Anthony pointed out) you don’t need to have a specific variable name for each one. It would be easier to give advice if we understood exactly what you are trying to accomplish (again, pointed out by Anthony) :slight_smile:

Dear aamonkey and AnthonySterling,
Our friend Immerse has given me an even neater solution than jgetner, both answering my question(s) as I posted them. I assume you want to go deeper into my actual code to find my real problem. The problem is just that, have so many thousands of lines of code that I have lost control. This is trouble shooting, trying to make a readable output on my screen that shows me what I want to know about my objects, alternatively what should have been objects, but aren’t. In order to do that I realized I lacked some programming tools, which Immerse, in particular, have presented to me. Thanks for your concern, but for the moment it seems like I’ve got what I need.

Many thanks, all of you!