for better way to make objects

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
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

for better way to make objects

Post by crystal ship »

Code: Select all

define ('DIRECTORY','../../../cis');
include ('queue.php');

function using($pathTree=NULL){
	$objQueue = new Queue;
	$objQueue->Queue();
	$path=ereg_replace("\.","/",$pathTree).".php";	
	search($path,DIRECTORY,&$objQueue);
}
In the above code snippet I have to make $objQueue in every other functions that uses the search function inside it and also have to create a new stack. I have tried creating both object and queue outside the function using but that dose not work. Could any one tell me the right way to handle this case.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: for better way to make objects

Post by webspider »

crystal ship wrote:

Code: Select all

$objQueue = new Queue;
	$objQueue->Queue();
is there any need to explicit call to the constructor? isn't Queue( ) constructor calling twice?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

define ('DIRECTORY','../../../cis');
include ('queue.php');

function using($pathTree=NULL){
$objQueue = new Queue;
$objQueue->Queue();
$path=ereg_replace("\.","/",$pathTree).".php";
search($path,DIRECTORY,&$objQueue);
}
I didn't understood what exactly is the problem and what are you trying to achieve, but you have syntax error in your code. I think following would be correct:

Code: Select all

define ('DIRECTORY','../../../cis');
include ('queue.php');

function using($pathTree=NULL){
   $objQueue = new Queue();
   $path=ereg_replace("\.","/",$pathTree).".php"; 
   search($path,DIRECTORY,$objQueue);
}

function search($path, $folder, &$queue) {
   ....
}
Have you enabled error reporting?
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Code: Select all

define ('DIRECTORY','../../../cis');
include ('queue.php');

function using($pathTree=NULL){
   $objQueue = new Queue();
   $path=ereg_replace("\.","/",$pathTree).".php";
   search($path,DIRECTORY,$objQueue);
}

function search($path, $folder, &$queue) {
   ....
}
This code is working well but the problem is if I have to make another function like usingJs() for using javascript file then I again have to create an object inside the function usingJs. Similarly I have to create object in every function whereever I call the function search. Is there any way that I can solve it by creating it only once somewhere outside the function so that I can use the same object passing as a parameter.
Post Reply