Cannot redeclare class action

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

siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Cannot redeclare class action

Post by siefkencp »

Ok so here goes trying to explain what I'm up to and how i got this message:

Code: Select all

Fatal error: Cannot redeclare class action in ----- on line 30
I have myself a little OO inheritance scheme going on...

base, class feeds
user
job
ui
ect...


I am trying to use methods accross the classes... Works fine except for when I inadvertantly try to instantiate a class thats already been invoked by an existing method... It doesnt seem to matter outside of Classes IE in the actual code...

Is this a flaw? or am I crazy for trying to pull things off like this?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

some code to look at may help us give you answers to your questions.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

These 2 have 'base' as a parent---

I dont know if this will just farther confuse things though

Code: Select all

require_once "base.class.php";
require_once "user.class.php";

class action extends base {

function return_pending_actions($parent_id){
	$user = new user();
	$uid = $user->return_user();
	$this->db_conn();
	$result = mysql_query("select * from actions 
	where sent_to = '$uid' 
	AND date_completed = 0 
	AND type = '1' 
	AND parent_id = '$parent_id'") 
	or die(mysql_error());
	return $result;
}
}

Code: Select all

function return_action_list($action_result) {
	$user = new user();
	$display = "<table>";
	while($result_row = mysql_fetch_array($action_result, MYSQL_ASSOC)){
		$action_id = $result_row['action_id'];
		$type = $result_row['type'];
		$create_by = $result_row['create_by'];
		$sent_to = $result_row['sent_to'];
		$date_created = $result_row['date_created'];
		$date_completed = $result_row['date_completed'];
		
		if($date_completed == 0){ $date_completed = "Pending"; }
		$created_by = $user->return_fname($create_by);
		$sent_to = $user->return_fname($sent_to);
		
		$date_created = $this->return_date($date_created);
		
	$display .= "<tr><td>$action_id</td><td>$created_by</td>
	<td>$sent_to</td><td>$date_created</td><td>$date_completed</td></tr>";
	}
	$display .= "</table>";
	return $display;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sorry, your code doesn't tell me much. What includes the action class' file? What includes that file? (and so on and so on.)
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Not to mention actually showing us line 30 from the relevant script...
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Im useing the require to handle my inheritence...
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

The issue here is instantiating user 2x in 2 different classes accross the hirerachy as opposed to down it using $this->Method()

There isnt really anything to 'show'... Flame me if you want to, ask me better questions if you don't understand.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Fatal error: Cannot redeclare class action in ----- on line 30
line 30 in ----- is exactly what?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Require/include have little to do with inheritance. Technically, they have nothing to do with it. Nor is inheritance the issue at hand. Somewhere along the line you either have two classes named "action" that come into existance during the process or running your code or you include "action"'s file more than once.

That's the explanation and the answer. How that applies to your code, I can't tell.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Ok, thank you.

I wasn't really looking for it as an aplication to my code persay, more of a fundimental answer would have been fine. Basically I cant instantiate a class under the same variable name 2x and expect that the old object be destroyed by the new one.

So, is there a way to distroy an object in php.

Line 30 in this case is $user = new user();

I think what i'm doing might just be bad practice but it would be nice to get a definitive answer to it...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I get the sense that there is something wrong with the design of your code, but can't really tell from what you have posted.

You cannot remove a class once it has been included/defined, but you can do this to prevent reloading classes of the same name:

Code: Select all

if (! class_exists('user')) {
     include "user.class.php";
}
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I cannot give a definitive answer given the code you've posted thus far points nowhere.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Ok, Here is the 5 complete files:

the first base.class.php (parent class)

Code: Select all

class base {

var $link;

function db_conn () {
	$link = mysql_connect("localhost","root","") or die(mysql_error());
	mysql_query("use ev2")or die(mysql_error());
	return "<!--db connection opened -->";
	}

function db_close () {
	mysql_close($link);
	}
		
function return_date($time){
	$date = date("m/d/y h:i.s",$time);
	return $date;
}

}
2nd file action.class.php

Code: Select all

require_once "base.class.php";
require_once "user.class.php";

class action extends base {

function insert_action($parent_id,$type_id,$create_by,$sent_to,$detail){
	$this->db_conn();
	$time = time();
	mysql_query("INSERT INTO actions 
	(parent_id,type,create_by,sent_to,date_created,description) 
	VALUES 
	('$parent_id','$type_id','$create_by','$sent_to','$time','$detail')") or die(mysql_error());
}

function return_pending_actions($parent_id){
	$user = new user();
	$uid = $user->return_user();
	$this->db_conn();
	$result = mysql_query("select * from actions 
	where sent_to = '$uid' 
	AND date_completed = 0 
	AND type = '1' 
	AND parent_id = '$parent_id'") 
	or die(mysql_error());
	return $result;
}


}
3rd file user.class.php

Code: Select all

require_once "base.class.php";

class user extends base {

	private $user;
	private $n;
	private $result;
	private $result_row;
	private $pc;
	private $fname;
	private $lname;

public function auth () {

	$user = $this->return_user();	
	
	if (!$user) { 
	die("you arent logged in --- Log in <a href=\"/\">HERE</a>"); 
	}
}

	//Return user id from stored cookie.

public function return_user() { 
	$user = $_COOKIE["user"];
	return $user;
}
	
public function log_in($user){
	$this->db_conn();
	$result = mysql_query("select * from users where uid = '$user'");
	$n = mysql_num_rows($result);

	if ($n == 1) {
		setcookie("user", $user, time()+3600, "/");
	}	

}

//returns a user pc
public function return_pc($user){
	$this->db_conn();
	$result = mysql_query("select pc_id from users where uid = '$user'");
	$result_row = mysql_fetch_array($result);
	$pc = $result_row[0];
	return $pc;
}
	
public function return_fname($user){
	$this->db_conn();
	$result = mysql_query("select fname from users where uid = '$user'");
	$result_row = mysql_fetch_array($result);
	$fname = $result_row[0];
	return $fname;
}
public function return_lname($user){
	$this->db_conn();
	$result = mysql_query("select lname from users where uid = '$user'");
	$result_row = mysql_fetch_array($result);
	$lname = $result_row[0];
	return $lname;
}

public function pop_up ($user,$msg) {
	$pc = $this->return_pc($user);
	$fp = fsockopen("udp://$pc",12001);
	fwrite($fp, "$msg");
        fclose($fp);
}
public function return_users() {
	$test = $this->db_conn();
	$result = mysql_query("select * from users") or die(mysql_error());
	return $result;
}
}
4th file ui.class.php

Code: Select all

require_once "base.class.php";
require_once "user.class.php";
require_once "action.class.php";

class ui extends base {

private $display;

function return_ui_openform($action) {
	$display = "<table border=\"1\"><tr><td><form action=\"$action\">";
	return $display;
}

function return_ui_closeform() {
	$display = "</form></td></tr></table>";
	return $display;
}

function return_ui_submit($button) {
	$display = "<tr><td align=\"right\" colspan=\"2\"><input type=\"submit\" name=\"Submit\" value=\"$button\"></td></tr>";
	return $display;
}
	
function return_ui_user_dropdown($result){
	$display = "<select name=\"selected_user\">";
	while($result_row = mysql_fetch_array($result, MYSQL_ASSOC)){
		$fname = $result_row['fname'];
		$lname = $result_row['lname'];
		$uid = $result_row['uid'];
		$display .= "<option value=\"$uid\">$fname $lname</option>";
		}
	$display .= "</select>";
	return $display;
	}
	
function return_ui_clarification_types_dropdown($result){
	$display = "<tr><td>Select Clarification Type:</td><td><select name=\"type_id\">";
	while($result_row = mysql_fetch_array($result, MYSQL_ASSOC)){
		$type_name = $result_row['type_name'];
		$typ_description = $result_row['type_description'];
		$type_id = $result_row['type_id'];
		$display .= "<option value=\"$type_id\">$type_name</option>";
		}
	$display .= "</select></td></tr>";
	return $display;
	}

function return_footer() {
	$display =  "</td></tr></table>";
	return $display;	
	}

function return_header($title,$fname){
	$display = "
	<table width=\"80%\" border=\"1\" align=\"center\" cellspacing=\"1\" cellpadding=\"1\" bgcolor=\"#003e69\">
	<tr><td width=\"10%\"><font color=\"white\"><center>$fname</center></font></td>
	<td><font color=\"white\" size=\"+1\"><center>$title</center></font></td></tr>
	<tr>
	<td nowrap>" . $this->return_mainmenu() . "</td>
	<td bgcolor=\"#ffffff\">";
	return $display;
	}

function return_mainmenu() {
	$this->db_conn();
	$display = "";
	$result = mysql_query("select * from menu_items");
	while($result_row = mysql_fetch_array($result, MYSQL_ASSOC)){
		$link = $result_row['link'];
		$title = $result_row['title'];
		$display .= "<a href=\"$link\"><font color=\"orange\">$title</font></a><br>";
	}
	return $display;
}

function return_ui_formbox($title,$var) {
	$display = "<tr><td>$title</td><td><input name=\"$var\"></td></tr>";
	return $display;
}

function return_ui_note($title,$var){
	$display = "<tr><td>$title</td><td><textarea name=\"$var\"></textarea></td></tr><br>";
	return $display;
}

function return_action_list($action_result) {
	$user = new user();
	$display = "<table>";
	while($result_row = mysql_fetch_array($action_result, MYSQL_ASSOC)){
		$action_id = $result_row['action_id'];
		$type = $result_row['type'];
		$create_by = $result_row['create_by'];
		$sent_to = $result_row['sent_to'];
		$date_created = $result_row['date_created'];
		$date_completed = $result_row['date_completed'];
		
		if($date_completed == 0){ $date_completed = "Pending"; }
		$created_by = $user->return_fname($create_by);
		$sent_to = $user->return_fname($sent_to);
		
		$date_created = $this->return_date($date_created);
		
	$display .= "<tr><td>$action_id</td><td>$created_by</td>
	<td>$sent_to</td><td>$date_created</td><td>$date_completed</td></tr>";
	}
	$display .= "</table>";
	return $display;
}


function return_clarification_list($result) {

		
	$display = "<table border=\"1\">";
	while($parent_row = mysql_fetch_array($result, MYSQL_ASSOC)){
	$clarification_id = $parent_row['clarification_id'];
	$created_by = $parent_row['create_by'];
	$date_created = $parent_row['date_created'];
	$date_created = $this->return_date($date_created);
	$status = $parent_row['status'];
	$job_id = $parent_row['job_id'];
	$clarification_type = $parent_row['clarification_type'];
	$created_by = $user->return_fname($created_by);
	
	$action = new action();
	$actions = $action->return_pending_actions($clarification_id);

	$display .= "<tr><td>$clarification_id</td><td>$job_id</td>
	<td>$created_by</td><td>$clarification_type</td><td>$status</td></tr>";
	}
	$display .= "</table>";
	return $display;
	}
}
5th file! this is tha actuil page main.php

Code: Select all

<?

require "inc/user.class.php";
require "inc/ui.class.php";
require "inc/clarification.class.php";
require "inc/action.class.php";

$title = "Start";

$user = new user();
$user->auth();
$uid = $user->return_user();
$fname = $user->return_fname($uid);

$clarification = new clarification();
$open_clars = $clarification->return_open_clarifications();

$action = new action();
$open_actions = $action->return_pending_actions($uid);


$ui = new ui();
$header = $ui->return_header($title,$fname);
$footer = $ui->return_footer();
$action_list = $ui->return_clarification_list($open_clars);


print $header;
print $action_list;
print $footer;

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

main.php is the culprit. Why? Because it includes ui.class.php which includes action.class.php, shortly after main.php includes action.class.php.

ui.class.php only needs to include action.class.php. main.php only needs to include ui.class.php and clarification.class.php.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Thats the cool thing about require_once... http://www.php.net/manual/en/function.require-once.php

The file itself is only included 1x regardless of how many calls so you can always ensure your class is available. everything works fine untill i actually instantiate user 2x in the action and ui classes above main.

Chris
Post Reply