Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whitedragon101

macrumors 65816
Original poster
Sep 11, 2008
1,337
334
I trying to learn the Class syntax for PHP and I have produced this simple example of setting a class variable and then getting it. It returns the error :

"$prop1 undefined variable "

Anyone know whats wrong? Thanks

Code:
class MyClass
{
  public $prop1 = "I'm a class property!";

  public function getProp1()
  {
    return $this->$prop1;
  }
}

$obj = new MyClass;

echo $obj->getProp1();

Server php version 5.2.17
 
Last edited:

whitedragon101

macrumors 65816
Original poster
Sep 11, 2008
1,337
334
I trying to learn the Class syntax for PHP and I have produced this simple example of setting a class variable and then getting it. It returns the error :

"$prop1 undefined variable "

Anyone know whats wrong? Thanks

Code:
class MyClass
{
  public $prop1 = "I'm a class property!";

  public function getProp1()
  {
    return $this->$prop1;
  }
}

$obj = new MyClass;

echo $obj->getProp1();

Server php version 5.2.17


.......and

Turns out its one of those tiny wheres wally syntax issues. Need to take out the variable symbol for class variables

$this->$prop1;

$this->prop1;
 
  • Like
Reactions: Xenc and AphoticD

294307

Cancelled
Mar 19, 2009
567
315
There is a lot I could say about the class you've written, but please get away from PHP 5.2. Upgrade to PHP 7.2, as a lot of the new features in PHP 7.x are relevant to your journey to object-oriented programming.

If it's your hosting provider that's limiting you to PHP 5.2, find a better hosting provider.
 
Last edited:

rafark

macrumors 68000
Sep 1, 2017
1,746
2,943
@whitedragon101
If you have a getter then the property shouldn't be public.

I highly recommend Matt Zandstra's "PHP Object Patterns and practice" book.
[doublepost=1524961534][/doublepost]
.......and

Turns out its one of those tiny wheres wally syntax issues. Need to take out the variable symbol for class variables

$this->$prop1;

$this->prop1;
This is a variable variable, only static props have a preceding $.
 
  • Like
Reactions: Xenc

whitedragon101

macrumors 65816
Original poster
Sep 11, 2008
1,337
334
There is a lot I could say about the class you've written, but please get away from PHP 5.2. Upgrade to PHP 7.2, as a lot of the new features in PHP 7.x are relevant to your journey to object-oriented programming.

If it's your hosting provider that's limiting you to PHP 5.2, find a better hosting provider.

Thanks. My current hosting provider where I do my development is just host.

Its weird they seem to mark the php7.0 option as beta. So I chose the highest option that didn't say beta on it.


mMvMgx
 

294307

Cancelled
Mar 19, 2009
567
315
OK, well there's two things you need to do:
  • Find a better web hosting provider that doesn't advertise PHP 7 as beta software to its customers.
  • Get a local development environment. If you're familiar enough, I would recommend using a Vagrant box (such as Homestead or a vanilla Ubuntu box) but if that's beyond your comfort zone right now, use MAMP at the very least.
 
  • Like
Reactions: whitedragon101

whitedragon101

macrumors 65816
Original poster
Sep 11, 2008
1,337
334
OK, well there's two things you need to do:
  • Find a better web hosting provider that doesn't advertise PHP 7 as beta software to its customers.
  • Get a local development environment. If you're familiar enough, I would recommend using a Vagrant box (such as Homestead or a vanilla Ubuntu box) but if that's beyond your comfort zone right now, use MAMP at the very least.

Thanks. I am looking for a better host but the trouble is searching for a good web host is so difficult because its so full of lies. Fake review sites fake reviews etc. Its hard to know who to trust. Host sites are experts at google ranking and gaming the system so its a very difficult one to drill down to some truth.

I'd ideally like a shared host that has a nice cpanel with all the trimmings and a good structure for growing as needed.

-shared host basic
-shared host with more resources (lower site load)
-shared host with premium resources (super low site load)
-vps low end
-vps faster
-cloud

(I have MAMP for local)
 

294307

Cancelled
Mar 19, 2009
567
315
Thanks. I am looking for a better host but the trouble is searching for a good web host is so difficult because its so full of lies. Fake review sites fake reviews etc. Its hard to know who to trust. Host sites are experts at google ranking and gaming the system so its a very difficult one to drill down to some truth.

I'd ideally like a shared host that has a nice cpanel with all the trimmings and a good structure for growing as needed.

-shared host basic
-shared host with more resources (lower site load)
-shared host with premium resources (super low site load)
-vps low end
-vps faster
-cloud

(I have MAMP for local)

Well, I personally recommend Namecheap but I understand your dilemma. You could say the same about my recommendation though... how can you trust it? You could try asking people directly for their recommendations... usually people will recommend a hosting company (or advise you stay away from) based on their own personal experience. Ask on IRC and on different forums where hosting reps don't lurk around. See which hosting company keeps getting endorsed and what the common themes are with the company/companies that are recommended the most.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.