Page 1 of 1

using new inside a class

Posted: Thu Nov 13, 2003 9:47 pm
by Sevengraff
Okay, I thought I could do this:

Code: Select all

<?PHP

class user {
   var $db = new myDatabaseClass();
    function user( &$sql ) {
        $this->db = $sql;
    }
}

$mysql = new MyDatabaseClass( $connection_info );
$user = new user( $mysql );

?>
But I am getting this:
Parse error: parse error, unexpected T_NEW in d:\program files\apache group\apache\htdocs\nlb3\system\user.class.php on line 25
Where line 25 is where I have my var $db = ....
I can declare var's with array(), why is it a problem to use new?
I am including the files in the needed order, so PHP knows that my database class exists. I looked through the manual on using new, but didn't see anything.

Posted: Fri Nov 14, 2003 12:53 pm
by Weirdan
Just because PHP works in this way ;) You can create instance of myDbClass in the constructor of user:

Code: Select all

<?PHP 

class user { 
   var $db;
    function user( &$sql ) { 
        $db = new myDatabaseClass();
        $this->db = $sql; 
    } 
} 

$mysql = new MyDatabaseClass( $connection_info ); 
$user = new user( $mysql ); 

?>
Just noticed that your $db instance gets overwritten by the constructor parameter. So what's the purpose of creating the new instanse in the user class?

Posted: Fri Nov 14, 2003 1:01 pm
by Weirdan
I found this note in the php manual:
In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).
Because new instatiates the object calling its constructor, its return value cannot be considered constant.

Posted: Fri Nov 14, 2003 1:19 pm
by JPlush76
if it helps here is how I use objects in my classes...

Code: Select all

<?php
class user_class
{

   function user_class()
   {

	require_once('class_db.php');
	
	// INVOKE THE REQUIRED  OBJECTS
	$this->db = new db();
	$this->db->connect();
   }

   function test()
   {
     $result = $this->db->query("SELECT * FROM everything");
   }

} // END SAMPLE CLASS
?>
if you do it in the constructor you can have access to it using $this->OBJECT->METHOD

Posted: Fri Nov 14, 2003 2:27 pm
by McGruff
Just to add a little, instantiating an object within another class is what's known as the factory pattern - see phppatterns.com for more on this and other patterns.

A very useful trick is to pass the creator object as an arg to the product object, eg:

Code: Select all

<?php

class MyClass
{
    function doSomething()
    {
        $ob = new Class($this);
    }
}
?>
This gives you access to all the creator properties and methods in the product object, if you need them.

PS: you have to get the referencing right for this to work correctly.

Posted: Fri Nov 14, 2003 2:42 pm
by Sevengraff
Thanks, you were all very helpful. I havn't used objects inside objects before.