![]() |
abstract classes and constructors
I've set up an abstract class to manage the collection of data from mysql tables and format them into tables before printing them out. As it stands the abstract class isnt doing much at present but i will be extending it with several implementation classes however all implementation classes with require input of a variable $username. Therefore I want to declare a constructor method to accept in username for every implementation class. Doesnt seem to work. I;ve tried moving the constructor down into the implementation class and still doesnt work always get the warning "Notice: Undefined variable: username" and it seems to run the query $query = "select username from login where (loggedin > 0) AND (username != '$username')"; ignoring the last section (username != '$username') however if i hardcode the value in instead of $username works fine. Am i doing something incorrect with classes or is this just a schoolboy syntax error i cant see. Any help greatly appreciated. Code below:
PHP Code:
|
Two minor issues. Firstly, You're just using
$username in writeTable, a local variable which is not defined anywhere. Secondly, $username is declared as private in the TableWriter abstract class, so child classes (OnlineNowTable cannot make use of it. |
Hi thanks for getting back to me. I've declared $username as public in TableWriter but not sure what you mean "You're just using $username in writeTable, a local variable which is not defined anywhere" - i still cant get this to work. Something about the way i am declaring and trying to use $username. Even if i try and print it out in the construct method after i've assigned it a value i still always get "Notice: Undefined variable: username".
|
What Salathe was saying is that you have to use $this->username instead of $username. The first one is a class member, the latter is just a local variable. Then, $this->username does not have to be public, it can be protected. That way, it has kind of "private" visibility which is also inherited to children, but cannot be overwritten from outside the class.
|
Sorry I still dont understand. Apologies i'm a beginner. have tried everything and still doesnt work. Could you correct the code for me and repost and then maybe i'll understand. PS - appreciate the help. Cheers.
|
PHP Code:
|
Apologies still cant get this to work. Any advice? I am now getting the message "Fatal error: Using $this when not in object context" when i use $this->username
Here's the onject instantiation client code: PHP Code:
PHP Code:
|
Can anyone spot any bugs in the code above?
|
That is because you have not instantiated the object, therefore $this will never contain the reference to the calling object (as it doesn't exist) instead you are using a static call to a class method (which in your case isn't declared static which is bad practice).
You would need something like this: PHP Code:
PHP Code:
|
Now works fine. Great thanks for your help. I had no intention of using a static method i just forgot to instantiate the object pretty basic mistake.
|
| All times are GMT. The time now is 04:59 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0