Jump to content

PHP class variables


RIRedinPA

Recommended Posts

I am getting the following error:

 

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /Library/WebServer/Dev/classtest/lib/class_userdata.php on line 5

 

on this class code:

 

class userdata { 

public $name = ""; 
public $city = "";
public $phone = "";

function setData($n, $c, $p) { 

	$name = $n;
	$city = $c;
	$phone = $p;

}

function getData() {

	 $userdata=array('name'=>$name, 'city'=>$city, 'phone'=>$phone); 
	 return $userdata; 

}

}

 

I thought that was a proper way to set up some class variables. (http://www.victorchen.info/accessing-php-class-variables-and-functions/). Thanks.

Link to comment
https://forums.phpfreaks.com/topic/223291-php-class-variables/
Share on other sites

There is nothing wrong with your variable declarations, but:

 

Inside a class you need to access the class properties through $this-> or static properties trough self::

 

class userdata {

   public $name = "";
   public $city = "";
   public $phone = "";

   function setData($n, $c, $p) {
      
      $this->name = $n;
      $this->city = $c;
      $this->phone = $p;
      
   }
   
   function getData() {
   
       $userdata=array('name'=>$this->name, 'city'=>$this->city, 'phone'=>$this->phone);
       return $userdata;
   
   }

}

That would be the appropriate usage of your variables.

Link to comment
https://forums.phpfreaks.com/topic/223291-php-class-variables/#findComment-1154318
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.