Page 1 of 1

__construct() question and class variable questions.

Posted: Tue Nov 17, 2009 9:58 am
by MonkOfox
Ok, so if in a class I use __construct() like so:

class p {

function __construct()
{echo ('constructed');}

}

does that function get called when I do:

$p = new p();

?

Also, I kept getting 'unexpected var, expecting old_function...'
when I would do the following:

class p{
public $Name;
}

but if I just used 'var $Name' it would work fine. do you have to put 'Public var $Name;' or something?

Thanks,

Justin

Re: __construct() question and class variable questions.

Posted: Tue Nov 17, 2009 10:11 am
by iankent
MonkOfox wrote:does that function get called when I do:
$p = new p();
Easy answer this one... yep :)
MonkOfox wrote: Also, I kept getting 'unexpected var, expecting old_function...'
Just tried the following code in nusphere and it works, so not sure why yours isnt:

Code: Select all

<?php
  class p{
    public $Name;
    
    function __construct() {
        $this->Name = 'test';   
    }
  }
  
  $x = new p();
  echo $x->Name;
?>
 

Re: __construct() question and class variable questions.

Posted: Tue Nov 17, 2009 10:54 am
by MonkOfox
this is my guild.php file that i get the error on. Line 5.

Code: Select all

 
<?php
 
class GuildMember
{ 
public $Name;
var $Level;
var $Class;
var $Race;
var $APs;
var $Rank;
var $AL;
 
//****************************
// set functions for class GuildMember
 
  function setName($value)
  { $this->Name = $value; }
 
  function setLevel($value)
  { $this->Level = $value; }
 
  function setClass($value)
  { $this->class = $value; }
 
  function setRace($value)
  { $this->Race = $value; }
 
  function setAPs($value)
  { $this->APs = $value; }
 
  function setRank($value)
  { $this->Rank = $value; }
 
  function setAL($value)
  { $this->AL = $value; }
 
//*****************************
//  get functions for class GuildMember
 
  function getName()
  { return $this->Name; }
 
  function getLevel()
  { return $this->Level; }
 
  function getClass()
  { return $this->class; }
 
  function getRace()
  { return $this->Race; }
 
  function getAPs()
  { return $this->APs; }
 
  function getRank()
  { return $this->Rank; }
 
  function getAL()
  { return $this->AL; }
 
 
  function getRowHtml()
  {
    $str  = "<tr onmouseover=\"style.backgroundColor = '#A52A2A';\" onmouseout=\"style.backgroundColor = '#8B0000';\" style=\"background-color:;\">";
        $str .= "<td>".$this->getName()."</td>";
    $str .= "<td>".$this->getRace()."</td>";
        $str .= "<td>".$this->getClass()."</td>";
        $str .= "<td id = \"Numeric\">".$this->getLevel()."</td>";
        $str .= "<td id = \"Numeric\">".$this->getAps()."</td>";
        $str .= "<td>".$this->getRank()."</td>";
        $str .= "<td><a href=\"".$this->getAL()."\" target=\"_blank\">Character-info</a></td>";
        $str .= "</tr>";
        return $str;
  }
 
 
}
 
?>
 
this is the error:
but if I use 'var' instead of 'public' or 'private' works just fine.

Code: Select all

 
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in Guild.php on line 5
 

Re: __construct() question and class variable questions.

Posted: Tue Nov 17, 2009 10:58 am
by iankent
Not sure sorry, works fine for me. Can instantiate the class and call the setName() function and echo out the value of $Name, as follows:

Code: Select all

 
$g = new GuildMember();
$g->setName("test");
echo $g->Name; // outputs 'test'
 
Not the answer you're looking for, but perhaps there's a php configuration setting somewhere that could be causing it? not sure what that'd be though, but your code is definately working!!

edit:
just found this on another site:
Looks to me like you're trying to use the "private" declaration in a PHP4 environment, perhaps? (public|private|protected are only available in PHP5, so you'll just have to use "var" if it's PHP4.)
is that it?

Re: __construct() question and class variable questions.

Posted: Tue Nov 17, 2009 11:06 am
by MonkOfox
yeah that's most likely the problem, a version error. I am using a 1and1 server, may have to call them to update to the new version.

I was just wanting to confirm that version error may be the problem.

So if I just use var, does that make the variable have public scope to the class object? I definitely wanted to use class protocol when creating any of my classes in php. I also noticed that if I didnt even declare my variables in my class that the functions still set() and get() the variable as if it was: i.e. just comment out the var declarations.

Thanks again,

Justin

Re: __construct() question and class variable questions.

Posted: Tue Nov 17, 2009 11:19 am
by iankent
php is 'easy' like that, doesn't care too much about whether things are declared properly or not. I'd personally always use var to err on the side of caution, and if you can get upgraded to PHP5 then use public and private to make it explicit.

not sure how PHP4 handles it, but in PHP5 a 'var' is generally equivalent to a 'public'

if you want to confirm its a version issue, run phpinfo() to check which PHP version you're running on

hth