30 second php question - whats wrong with this most basic class?

Discussion in 'Web Design and Development' started by whitedragon101, Apr 9, 2018.

  1. whitedragon101, Apr 9, 2018
    Last edited: Apr 9, 2018

    whitedragon101 macrumors 65816

    Joined:
    Sep 11, 2008
    #1
    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
     
  2. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #2

    .......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;
     
  3. bbnck, Apr 28, 2018
    Last edited: Apr 28, 2018

    bbnck macrumors 6502a

    Joined:
    Mar 19, 2009
    #3
    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.
     
  4. rafark macrumors 6502a

    rafark

    Joined:
    Sep 1, 2017
    #4
    @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.
    --- Post Merged, Apr 28, 2018 ---
    This is a variable variable, only static props have a preceding $.
     
  5. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #5
    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.

    [​IMG]
    [​IMG]
     
  6. bbnck macrumors 6502a

    Joined:
    Mar 19, 2009
    #6
    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.
     
  7. whitedragon101 thread starter macrumors 65816

    Joined:
    Sep 11, 2008
    #7
    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)
     
  8. bbnck macrumors 6502a

    Joined:
    Mar 19, 2009
    #8
    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.
     

Share This Page