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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

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

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Yeah, i typed it wrong, sorry about that. That wasn't the cause of the problem though.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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'

?>
Post Reply