|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I'm trying to learn how PHP classes work. I'm very familiar with C++ and such, but something that I thought should work is giving me grief. Basically, this is what I have. class myclass { private $somevar; public __construct($value) { $this->$somevar = $value; } } Then, in my other code I have: require("path/to/library.php"); $obj = new $myclass("assign it this"); When I attempt to load this page in my browser, I'm greeted with this error: Cannot access empty property in /usr/local/www/data-dist/mch/library/ generic_table.php on line 19 This is coming from the variable assignment in my c-tor in the class definition. Why isn't this working and how would I make it work? Andy |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Andrew Falanga wrote:
> Hi, > > I'm trying to learn how PHP classes work. I'm very familiar with C++ > and such, but something that I thought should work is giving me > grief. Basically, this is what I have. > > class myclass { > private $somevar; > > public __construct($value) { > $this->$somevar = $value; > } > } > > Then, in my other code I have: > > require("path/to/library.php"); > > $obj = new $myclass("assign it this"); > > > When I attempt to load this page in my browser, I'm greeted with this > error: > > Cannot access empty property in /usr/local/www/data-dist/mch/library/ > generic_table.php on line 19 > > This is coming from the variable assignment in my c-tor in the class > definition. Why isn't this working and how would I make it work? > > Andy > Andy, Just minor problem - but a common one. It should be $this->somevar = $value; -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 23, 2:43 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Andrew Falanga wrote: > > Hi, > > > I'm trying to learn how PHP classes work. I'm very familiar with C++ > > and such, but something that I thought should work is giving me > > grief. Basically, this is what I have. > > > class myclass { > > private $somevar; > > > public __construct($value) { > > $this->$somevar = $value; > > } > > } > > > Then, in my other code I have: > > > require("path/to/library.php"); > > > $obj = new $myclass("assign it this"); > > > When I attempt to load this page in my browser, I'm greeted with this > > error: > > > Cannot access empty property in /usr/local/www/data-dist/mch/library/ > > generic_table.php on line 19 > > > This is coming from the variable assignment in my c-tor in the class > > definition. Why isn't this working and how would I make it work? > > > Andy > > Andy, > > Just minor problem - but a common one. It should be > > $this->somevar = $value; > WOW! That was minor. Thanks much. I was nearly beginning to pull out what is left of my hair. Andy |
|
![]() |
| Outils de la discussion | |
|
|