Page 1 of 1

[SOLVED] static $property; not working as I think is suppose

Posted: Thu Jun 17, 2004 7:08 pm
by andre_c
I'm using php 5 and I'm having this problem and I'm not sure if I'm the one that's wrong or php is not doing what is supposed to:

Code: Select all

<?
class editor {
  
  static $pages = 'first';

  function __construct () {
     $this->pages = 'second';
  }

}

$test = new editor;

echo editor::$pages;    // prints 'first'

?>
Shouldn't it print second?
Am I wrong in assuming that it should print second?

Posted: Thu Jun 17, 2004 7:11 pm
by John Cartwright

Code: Select all

<?php
<? 
class editor { 
  
  static $pages = 'first'; // your missing a quote 

  function __construct () { 
     $this->pages = 'second'; 
  } 

} 

$test = new editor; 

echo editor::$pages;    // prints 'first' 

?> 
?>
EDIT: YOU WERE missing a quote.. guess u edited it

Posted: Thu Jun 17, 2004 7:13 pm
by andre_c
Yeah, i typed it wrong, sorry about that. That wasn't the cause of the problem though.

Posted: Thu Jun 17, 2004 7:35 pm
by andre_c
I solved it. I guess PHP is doing it like it's supposed to be done :D

to change a static property I have to do it like this:

Code: Select all

<?
class editor {
 
  static $pages = 'first';

  function __construct () {
     self::$pages = 'second';  // I have to edit this property on the class and not on the object
  }

}

$test = new editor;

echo editor::$pages;    // now it prints what it's supposed to: 'second'

?>