Jump to content


Photo

Working with Objects in different classes


  • Please log in to reply
11 replies to this topic

#1 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 20 July 2006 - 10:22 PM

I'm new to PHP, and I can't seem to figure this one out. I have three classes. The main class (class1) runs a couple switch statements to parse post data and to determine which instance of class2 is called to generate the main information on the page. Class3 has a bunch of standard methods and instructions I use on all my pages. This includes a debug method (method4) and corresponding variable (var1) to hold a string of debug statements.

How do I get my class/method references to work properly?

class class1 {

	var $class2;
	var $class3;
	
	function class1() {
		$class2 = new Class2;
		$class3 = new Class3;
		$class3->method3();
	}
	function method1() { 
		$class2->method2($this); 
	}
}

class class2 {
	var $var1 = 1;
	
	function class2() { 
		$var1 = 2; 
	}
	function method2($caller) { 
		$method3($caller); 
	}
	function method3($caller) { 
		$caller->class3->method4(); 
	}
}

class class3 {
	var $var1 = 1;
	
	function class3() {
		$var1 = 2; 
	}
	
	function method4() { 
		print ($var1); // want to have this print 2 
	}
}

$num1 = new class1;
$num1->method1();

PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 July 2006 - 10:30 PM

How do I get my class/method references to work properly?

Really... its difficulkt to see what it is your actually trying to do. I will just copy / paste your code and add some comments.

class class1 {

	var $class2;
	var $class3;
	
	function class1() {
		$class2 = new Class2;
		$class3 = new Class3;
		$class3->method3(); // there is no method3 defined in class3.
	}
	function method1() { 
		$class2->method2($this); 
	}
}

class class2 {
	var $var1 = 1;
	
	function class2() { 
		$var1 = 2; 
	}
	function method2($caller) { 
		$method3($caller); 
	}
	function method3($caller) { 
		$caller->class3->method4(); // this line makes NO sense at all.
	}
}

class class3 {
	var $var1 = 1;
	
	function class3() {
		$var1 = 2; 
	}
	
	function method4() { 
		print ($var1); // want to have this print 2 // see below.
                print $this->var1;
	}
}

$num1 = new class1;
$num1->method1();

Sorry, but its pretty hard to help when your examples are so far from correct I can't understand them.


#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 10:33 PM

what are the errors? you also have problem with capitalized Class2 and Class3 calls in function class1()
function class1() {
		$class2 = new class2; //should be lower case
		$class3 = new class3; // should be lower case
		$class3->method3();
}

hope this help
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 06:01 PM

I've done some revision on the idea myself, and come up with some new code. However, it still isn't working for me.

I'm getting: Fatal error: Call to a member function on a non-object in "..." on line ... related to the first line of supportClass->supportMethod(). I get this error whether or not "this->" is included.

<?php

class htmlGen {
	var $debugStr;
	
	function htmlGen() {
		$debugStr = "";
	}
	
	function debugFn($functionName, $string) {
		$this->debugStr .= "<div>$functionName: $string</div>/n";
	}
}

class supportClass {
	var $htmlGenSupport;
	
	function supportClass($html) {
		if(isset($html)) {
			$this->htmlGenSupport = $html;
		} else {
			$htmlGenSupport = new htmlGen();
		}
	}

	function internalMethod() {
		$this->htmlGenSupport->debugFn("internalMethod", "Successfully entered");
	}
	
	function supportMethod() {
		//$this->htmlGenSupport->debugFn("supportMethod", "Successfully entered"); // getting error message
		$htmlGenSupport->debugFn("supportMethod", "Successfully entered"); // for both cases
		$this->internalMethod();
	}
}

class myProgram {
	var $htmlGen;
	
	function myProgram($html) {
		if(isset($html)) {
			$this->htmlGen = $html;
		} else {
			$htmlGen = new htmlGen();
		}
	}
	
	function myProgMethod() {
		$this->htmlGen->debugFn("myProgMethod", "Successfully entered");
		$supportClass = new supportClass(&$html);
		$supportClass->supportMethod();
	}
	
	function run() {
		$this->htmlGen->debugFn("run", "Successfully entered");
		$this->myProgMethod();
	}
}

$classHTML = new htmlGen();
$classHTML->debugFn("open", "Externally generated htmlGen used");
$classMyProg = new myProgram(&$classHTML);
$classMyProg->run();
print "<table><tr><td colspan=2>The two columns should look the same.</td></tr>
<tr><td>$classHTML->debugStr</td>
<td><div>open: Externally generated htmlGen used</div>
<div>run: Successfully entered</div>
<div>myProgMethod: Successfully entered</div>
<div>supportMethod: Successfully entered</div>
<div>internalMethod: Successfully entered</div></td></tr></table>";
?>

Outside of the error, further suggestions that will clean it up would also be appreciated. The intention of the debugFn is, obviously, to generate a string of debug statements, rather than punctuate my page with them as I find the need. This also allows me to turn on and off debug statements easily.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 July 2006 - 07:22 PM

try this, I made a couple of changes (commented)
<?php
class htmlGen {
	var $debugStr;
	
	function htmlGen() {
		$debugStr = "";
	}
	
	function debugFn($functionName, $string) {
		$this->debugStr .= "<div>$functionName: $string</div>/n";
	}
}

class supportClass {
	var $htmlGenSupport;
	
	function supportClass($html=null) {                    // <-- set to null if no argument
		if($html) {
			$this->htmlGenSupport = $html;
		} else {
			$htmlGenSupport = new htmlGen();
		}
	}

	function internalMethod() {
		$this->htmlGenSupport->debugFn("internalMethod", "Successfully entered");
	}
	
	function supportMethod() {
		//$this->htmlGenSupport->debugFn("supportMethod", "Successfully entered"); // getting error message
		$this->htmlGenSupport->debugFn("supportMethod", "Successfully entered"); // for both cases
		$this->internalMethod();
	}
}

class myProgram {
	var $htmlGen;
	
	function myProgram($html=null) {
		if($html) {
			$this->htmlGen = $html;
		} else {
			$htmlGen = new htmlGen();
		}
	}
	
	function myProgMethod() {
		$this->htmlGen->debugFn("myProgMethod", "Successfully entered");
		$supportClass = new supportClass($this->htmlGen);   //<-- $this->htmlGen and NOT $html
		$supportClass->supportMethod();
	}
	
	function run() {
		$this->htmlGen->debugFn("run", "Successfully entered");
		$this->myProgMethod();
	}
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 07:52 PM

Okay, working with that, I'm able to get the program to run, but it isn't printing the expected output. Throwing in a bunch of print statements, I can tell that it runs through the methods as expected, but the object isn't copying to the variables as it's supposed to. Does php have a special way that it deals with copying objects, or will I have to write a function to do it?

Thanks for the help!

<?php

class htmlGen {
	var $debugStr;
	var $count = 0;
	
	function htmlGen() {
		print "1 ";
		$debugStr = "";
	}
	
	function debugFn($functionName, $string) {
		$count++;
		print "debug$count ";
		$this->debugStr .= "<div>$functionName: $string</div>/n";
	}
}

class supportClass {
	var $htmlGenSupport;
	
	function supportClass($html=null) {
		print "7 ";
		if($html != null) {
			print "8 ";
			$this->htmlGenSupport = $html;  // this copy isn't happenning
		} else {
			print "error ";
			$htmlGenSupport = new htmlGen();
		}
	}

	function internalMethod() {
		print "10 ";
		$this->htmlGenSupport->debugFn("internalMethod", "Successfully entered");
	}
	
	function supportMethod() {
		print "9 ";
		$this->htmlGenSupport->debugFn("supportMethod", "Successfully entered");
		$this->internalMethod();
	}
}

class myProgram {
	var $htmlGen;
	
	function myProgram($html=null) {
		print "3 ";
		if($html != null) {
			print "4 ";
			$this->htmlGen = $html; // this copy isn't happening
		} else {
			print "error ";
			$htmlGen = new htmlGen();
		}
	}
	
	function myProgMethod() {
		print "6 ";
		$this->htmlGen->debugFn("myProgMethod", "Successfully entered");
		$supportClass = new supportClass(&$this->htmlGen);
		$supportClass->supportMethod();
	}
	
	function run() {
		print "5 ";
		$this->htmlGen->debugFn("run", "Successfully entered");
		$this->myProgMethod();
	}
}

$classHTML = new htmlGen();
$classHTML->debugFn("open", "Externally generated htmlGen used");
print "2 ";
$classMyProg = new myProgram(&$classHTML);
$classMyProg->run();
print "<table border=1><tr><td>$classHTML->debugStr</td><td><div>open: Externally generated htmlGen used</div>
<div>run: Successfully entered</div>
<div>myProgMethod: Successfully entered</div>
<div>supportMethod: Successfully entered</div>
<div>internalMethod: Successfully entered</div></td></tr></table>";
?>

Output:

1 debug1 2 3 4 5 debug1 6 debug1 7 8 9 debug1 10 debug1

open: Externally generated htmlGen used
/n
  open: Externally generated htmlGen used
run: Successfully entered
myProgMethod: Successfully entered
supportMethod: Successfully entered
internalMethod: Successfully entered


PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 July 2006 - 08:04 PM

Depends on your version of PHP.

PHP5 always passes objects by reference.

PHP4 passes copies of the object unless you explicitly pass by ref with "&"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 08:10 PM

I'm working with php 4.3. Passing either with or without '&' leaves me with the same output (which really confuses me, since I had read up on it and that seemed like it would be the perfect fix).

For what seems like such a simple bug, this thing has taken about 8 hours away from me and is driving me nuts. It's looking like my best option is to just go with a globally declared variable and function rather than using the class, which I didn't want to do.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 July 2006 - 10:55 PM

On those two lines where you commented "this copy isn't happening" use

... = &$html

instead of

... = $html

PS or upgrade to v5. Itworks as expected in the new improved version
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 11:03 PM

I've switched it over to a global variable and function and it seems to be working now.

Unfortunately, this is for work, where I am the most senior developer (and I'm just a summer student), and the hosting is outsourced, so there isn't much hope of upgrading  >:(.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 July 2006 - 11:08 PM

That was a waste of my last hour then.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 24 July 2006 - 04:00 PM

Hopefully it'll help someone else out. And we both learned something at least. It was a nasty bug. I lost about 7 hours on it before giving up. But, now I (hopefully) have a better idea how to work with it if I ever run into the issue again.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users