Jump to content

Archived

This topic is now archived and is closed to further replies.

KittyKate

Working with Objects in different classes

Recommended Posts

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?

[code]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();[/code]

Share this post


Link to post
Share on other sites
[quote]How do I get my class/method references to work properly?[/quote]
Really... its difficulkt to see what it is your actually trying to do. I will just copy / paste your code and add some comments.

[code=php:0]
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();
[/code]

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

Share this post


Link to post
Share on other sites
what are the errors? you also have problem with capitalized Class2 and Class3 calls in function class1()
[code]function class1() {
$class2 = new class2; //should be lower case
$class3 = new class3; // should be lower case
$class3->method3();
}[/code]

hope this help

Share this post


Link to post
Share on other sites
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: [i]Fatal error: Call to a member function on a non-object in "..." on line ... [/i] related to the first line of supportClass->supportMethod(). I get this error whether or not "this->" is included.

[code]<?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>";
?>[/code]

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.

Share this post


Link to post
Share on other sites
try this, I made a couple of changes (commented)
[code]<?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();
}
}[/code]

Share this post


Link to post
Share on other sites
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!

[code]<?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>";
?>[/code]

Output:
[quote]1 debug1 2 3 4 5 debug1 6 debug1 7 8 9 debug1 10 debug1
[table][tr][td]open: Externally generated htmlGen used
/n[/td][td]  [/td][td]open: Externally generated htmlGen used
run: Successfully entered
myProgMethod: Successfully entered
supportMethod: Successfully entered
internalMethod: Successfully entered[/td][/tr][/table][/quote]

Share this post


Link to post
Share on other sites
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 "&"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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  >:(.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.