I'm working on a site that I did not originally program. I'm new to working with PHP classes.
There is a class on this website called Reporting and an extension called main. An instance of main (called $main) is created in the index page (all other pages are an include of the index page). I can't find any place that an instance of reporting is created.
What confuses me is from what I've read, it's my understanding, in order to access a class you must create an instance of that class like this
PHP Code:
<$main = new main()
Then you can reference aspects of main like this
PHP Code:
$main->displaypage()
However there are places all over the code for this site where functions and variables in the reporting class are referenced using a variable called $this, like this
PHP Code:
$this->set_value("type",$_POST['type'])
from what I can tell $this is a special variable intended to be used within the class it's self to reference things within the class.
Is this an appropriate use of $this?
everything seems to work ok (but I'm concenred there's a problem I'm not seeing or I'll break something if I don't understand it)
In order for this to be working, does that mean $this was created as an instance of the reporting calss some where?
Any explination is appericates.
I'm including the classes and an example refernce of this outside the clase incase that will help.
PHP Code:
class reporting {
var $errors = array(), $values = array(), $success = array();
function set_error ($name, $message) {
$this->errors[$name] = $message;
}
//from a file used to handle input from front end page
if (!is_uploaded_file($_FILES['file']['tmp_name']) || !is_image($_FILES['file']['tmp_name'])) {
$this->set_error('file', 'Please select a valid image to upload.');
}
$this is simply a construct that gives a developer access to the current instance of the class in reference. it's not really an object on its own, but more of a way for the developer to let the code talk to itself.
[QUOTE=chazzy;984152]$this is simply a construct that gives a developer access to the current instance of the class in reference.[QUOTE]
I aperciate the response but I think I need more clarification.
What you just said above is consistant with everything I read in the PHP manual about classes and the $this construct. However what I'm still not understanding is how in the code I'm working with they used it outside the context of a class.
You explination and the PHP manual makes it sound like a $this should only be used with in the context of a class. And the whole point is you can't have an instance of that class with in the class so you need some why to refernce things with in the class.
So why am I seeing it outside of class? Being treated like an instance of a class? But I can't find where that instance was created.
how can it be used outside of a class? How does the system know which class $this reffers to if it's not with in a class?
I can only think of two possilities.
1. The pervious programer did a really crazzy thing and intentionally created an instance of a class ( I assumeing reporting) and named that instance $this. And for some reason I can't find where he created it.
2. There's other way to use $this where it acts as some sort of variable instance of a class, like it refernces that last created instance ( which seems like really vauge code as well).
So am I right that $this should only be used with in a class?
IF so then any guesses how it's being used as a class instance?
IF not then how is it used outside of a class? What's referncing and how is that defined?
You should get a parse error if you try to assign anything to $this (at least you do in PHP5; I don't have a PHP4 version to test on), so it's hard to imagine how it could be used anywhere other than within a class definition. For instance, this script...
PHP Code:
<?php class Foo { public function bar() { echo "Hello, World!"; } }
$this = new Foo();
...generates this error...
Fatal error: Cannot re-assign $this in C:\wamp\www\test\test.php on line 10
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The $this variable, when used inside a definition for a class method is a pointer to the current instance of that class. If using $this outside of a class method definition you might be able to assign a variable called $this:
PHP Code:
<?php
class TestClass { private $foo = '';
public function __construct($foo) { // $this points to current instance of TestClass class $this->foo = $foo; } }
// $this is a variable named $this $this = "foo";
?>
I wonder if the code above generates an error? I guess I thought $this only had special meaning inside a class method definition as illustrated by the __construct function in the example above.
enad_26, more than likely $this is being used inside a class method definition and you aren't realizing it.
Last edited by toicontien; 03-02-2009 at 10:04 AM.
Reason: Spleling an grammer airors
toicontien-- I didn't see your post until after I figured it out. But you're right.
It does apear assigning $this as an instance of a class does work in PHP 4. Seems like a very bad idea but it works. Maybe that's why they made it not work in PHP 5.
But This site has worked on both PHP 5 and PHP 4. And when i tried to assign $this as an instance of a class on the PHP 5 server I got the same error.
So I messed with this for about an hour....
When suddenly the light bulb came on.
This whole site is actually loaded through the same PHP file. That file calls a function within a class to display all of the content that isn't reproduced on everypage.
So because every other include is, included through a class any class functions must be refernced with $this instead of the instance name.
Bookmarks