Casey Flynn
Casey Flynn

Reputation: 14038

Access class property inside class method

I have a simple PHP class, I'm trying to access a property of that class from a method in that class. I've tried the $this->property_name syntax and that doesn't appear to work. What am I doing wrong?

class NavigationBuilder {

    public $mPage;
    public $mGeoID;
    public $mContinent;
    public $mCountry;
    public $mRegion;

    private $Geograph = 'a';

    public function construct_navigation() {

    }

    public function __construct() {

        var_dump($Geograph);
    }

}

Upvotes: 4

Views: 3232

Answers (2)

Flipper
Flipper

Reputation: 2609

Try this:

class NavigationBuilder {

    public $mPage;
    public $mGeoID;
    public $mContinent;
    public $mCountry;
    public $mRegion;

    private $Geograph;

    public function construct_navigation() {
      $this->Geograph = 'a';
    }

    public function __construct() {

        var_dump($this->Geograph);
    }

}

Upvotes: 0

Neil Aitken
Neil Aitken

Reputation: 7854

You are correct that you need to use $this->property. Your example works if you use $this

<?php

class NavigationBuilder {

    public $mPage;
    public $mGeoID;
    public $mContinent;
    public $mCountry;
    public $mRegion;

    private $Geograph = 'a';

    public function construct_navigation() {

    }

    public function __construct() {

        var_dump($this->Geograph);
    }
}

// prints string(1) "a"
$geo = new NavigationBuilder();

Upvotes: 5

Related Questions