$post_code='22222'; and using $this->

<?php

/**
 * Park Address. 
 */
class AddressPark extends Address {
  public $postal_code='11111';
  public function display(){
    $this->postal_code='22222';
    $output = '<div style="background-color:PaleGreen;">';
    $output .= AddressResidence::display();
    $output .= '</div>';
    return $output;
  }
  
  /**
   * Initialization. 
   */
  protected function _init() {
    $this->_setAddressTypeId(Address::ADDRESS_TYPE_PARK);
  }
}

Open in new window


I looked at other tutorials and I do not understand that much, please do not show me another tutorial

only discussing altering line7

when $address_park is called from another file because of line7
the postal_code changes from '11111' to '22222'

but when in the display method when I write
public $post_code='22222';
the ide error underlines 'public'

and when I write
$post_code='22222';
then no change is made
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Inside a class definition there are three kinds of properties: private, protected and public.  If you do not designate a kind, public is the default.  If your IDE thinks there is something wrong with designating public, your IDE is wrong.

Private properties are properties that are visible and mutable inside the class that defined them.  Protected properties are properties that are visible and mutable inside the class that defined them and any classes that extend that class.  Public properties are visible and mutable in any scope that has access to any object of the class.

The special term $this-> is used to refer to properties and methods of the class.  The variable $x is only visible inside the method that defines it.  The variable $this->x is a property and is visible under the terms of public, protected or private.  In other words, it may be found and used in classes that extend the parent class, and in other scopes if it is a public property.
0
rgb192Author Commented:
sorry I meant line 9 (not line7)

sometimes my nusphere php ide is wrong
but in this case it spotted the error

when line 9 is
public $postal_code='22222';

Parse error: syntax error, unexpected T_PUBLIC in C:\wamp\www\Ex_Files_oophp-edit\05_04\class.AddressPark.inc on line 9


when line 9 is
$postal_code='22222';

no change



when line 9 is
$this->postal_code='22222';

I see output 22222

I also can not put
$this->postal_code='22222';
in the properties
on line 8 before the method


What I think is going on is
the only way to use postal_code in this class (within another method) is to call it $this->postal_code
0
Ray PaseurCommented:
What I think is going on is
the only way to use postal_code in this class (within another method) is to call it $this->postal_code
Yes, that is correct.  In the variable declaration, you omit the keyword $this-> but when you use the variable as a class property, you must use $this->.  It's confusing at first and one of the common PHP OOP errors is forgetting the $this-> prefix when referring to class properties and methods.  I make that mistake about twice a day.  Fortunately I have gotten so used to making the mistake that I know to be on the lookout for it!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Thank you for this easy explanation:

 In the variable declaration, you omit the keyword $this-> but when you use the variable as a class property, you must use $this->.
0
Ray PaseurCommented:
Exactly!  Quirky syntax, but the concept works well, and you have it right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.