Page 1 of 1
using $this to call a method?
Posted: Fri Apr 02, 2010 10:14 am
by miket3
I understand variable scopes. And I understand how to use $this to reference a global variable. I like idea of using $this instead of making global declarations in all of my methods. But I come to a dead end when I want to use $this to call a method....
How do I make this work?
$this->form =
$this->doc->createElement('xxx','xxxx');
Thanks,
Mike
Code: Select all
class dbFormClass {
var $doc;
var $form;
function __construct() {
global $doc, $form;
$doc = new DOMDocument('1.0', 'iso-8859-1');
$form = $doc->createElement('form', 'form goes here');
$doc->appendChild($form);
$el = $doc->createElement('test','new element');
$form->appendChild($el);
}
function set_name($name) {
global $doc, $form;
$form->setAttribute('name',$name);
}
function show_form() {
global $doc, $form;
echo $doc->saveHTML();
}
} // end dbFormClass
Re: using $this to call a method?
Posted: Fri Apr 02, 2010 10:36 am
by Weirdan
miket3 wrote:I understand variable scopes. And I understand how to use $this to reference a global variable.
$this does not reference any global variables, it references
current instance of the class. So, in fact, you do not understand its usage.
Code: Select all
class dbFormClass {
var $doc;
var $form;
function __construct() {
global $doc, $form; // this line shouldn't be here, remove
$doc = new DOMDocument('1.0', 'iso-8859-1'); // this should be $this->doc = new DomDocument(....);
$form = $doc->createElement('form', 'form goes here'); // this should be $this->form = $this->doc->createElement(....);
$doc->appendChild($form); // this should be $this->doc->appendChild($this->form);
$el = $doc->createElement('test','new element'); // this should be $el = $this->doc->createElement(....); ($el is a local variable).
$form->appendChild($el); // this should be $this->form->appendChild($el);
}
// .....
}
Re: using $this to call a method?
Posted: Fri Apr 02, 2010 10:53 am
by miket3
ok. thanks. As long as you confirmed that my syntax was correct I went back and found that I missed using $this in a spot. Actually, You missed the same exact instance that i missed!!!! Can you find it?
Re: using $this to call a method?
Posted: Fri Apr 02, 2010 11:42 am
by minorDemocritus
miket3 wrote:ok. thanks. As long as you confirmed that my syntax was correct I went back and found that I missed using $this in a spot. Actually, You missed the same exact instance that i missed!!!! Can you find it?
Don't try making people hunt for mistakes. It comes off as very smug, especially when the person is trying to help you. I have a feeling that either you don't know what you're talking about, or he left it out on purpose.
Weirdan gave you general tips in the code snippet he provided... I don't blame him at all for not rewriting ALL your code. It would be much more constructive to point out what you THINK he left out, and ask why he did, and if it's correct.
Here's what I got from Weirdan's reply:
1. Don't use global variables, especially in functions.
2. $this->doc references the property $doc in the current instance of the class (the object in the script that uses the class).
3. $this->doc->createElement( ... ) calls the method createElement() of the DOMDocument class inside the current instance (the $doc object inside the current instance of the dbFormClass).
That's a pretty thorough example of what you were doing wrong, and how to fix it. You should be able to understand OOP better because of it. I certainly do.
Re: using $this to call a method?
Posted: Fri Apr 02, 2010 12:17 pm
by miket3
You won't be able to hunt down the error because it was fixed by the replyer. so the answer no longer has the error.
Re: using $this to call a method?
Posted: Fri Apr 02, 2010 2:58 pm
by minorDemocritus
Rats. I guess that's what I get for being self-righteous.