using new inside a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

using new inside a class

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Wed Aug 10, 2005 10:10 am, edited 1 time in total.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Thanks, you were all very helpful. I havn't used objects inside objects before.
Post Reply