LSB (PHP 5.3) problem with static value!

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
keevitaja
Forum Newbie
Posts: 8
Joined: Wed Feb 25, 2009 7:46 am

LSB (PHP 5.3) problem with static value!

Post by keevitaja »


my question is PHP 5.3 specific! static binding wasn't present in previous versions!


hello,

i'm having a problem. static::$text variable gets lost at some point. can someone please correct and explain it to me?

Code: Select all

 
<?php
class A {
    protected static $text;
    
    public static function init($one = '', $two = '') {
        $obj = new self;
        
        $obj->one = $one;
        $obj->two = $two;
        $obj->static_text = static::$text;
        
        return $obj;
    }
    
    public function output_text() {
        echo 'here shoult be some static text: ' . static::$text;
    }
}
 
class B extends A {
    protected static $text = 'this is a static text under php 5.3';
}
 
$b = B::init('first', 'second');
 
echo "first: {$b->one}<br>second: {$b->two},<br>static text: {$b->static_text}<hr>"; //prints out the static_text
 
$b->output_text(); //doesn't print out the static::text value
 
echo '<hr>';
 
B::output_text(); //prints out static::$text value
 
echo '<hr>';
 
$b = new B;
$b->output_text(); //prints out static::$text value
?>
Last edited by keevitaja on Fri Oct 30, 2009 12:21 pm, edited 1 time in total.
keevitaja
Forum Newbie
Posts: 8
Joined: Wed Feb 25, 2009 7:46 am

Re: LSB (PHP 5.3) problem with static value!

Post by keevitaja »

http://www.php.net/manual/en/language.o ... ndings.php

it is static binding... check B::output_text() is working...

my question is PHP 5.3 specific! static binding wasn't present in previous versions!

it has no efect:

Code: Select all

   public static function output_text() {
        echo 'here shoult be some static text: ' . static::$text;
    }
keevitaja
Forum Newbie
Posts: 8
Joined: Wed Feb 25, 2009 7:46 am

Re: LSB (PHP 5.3) problem with static value!

Post by keevitaja »

i solved the problem:

Code: Select all

   public static function init($one = '', $two = '') {
        $obj = new static;
keevitaja
Forum Newbie
Posts: 8
Joined: Wed Feb 25, 2009 7:46 am

Re: LSB (PHP 5.3) problem with static value!

Post by keevitaja »

well this code is working 100%
so no more suggestions is needed.

Code: Select all

<?php
class A {
    protected static $text;
    
    public static function init($one = '', $two = '') {
        $obj = new static;
        
        $obj->one = $one;
        $obj->two = $two;
        $obj->static_text = static::$text;
        
        return $obj;
    }
    
    public function output_text() {
        echo 'here shoult be some static text: ' . static::$text;
    }
}
 
class B extends A {
    protected static $text = 'this is a static text under php 5.3';
}
 
$b = B::init('first', 'second');
 
echo "first: {$b->one}<br>second: {$b->two},<br>static text: {$b->static_text}<hr>"; //prints out the static_text
 
echo 'missing: ';
$b->output_text(); //doesn't print out the static::text value
 
echo '<hr>';
 
B::output_text(); //prints out static::$text value
 
echo '<hr>';
 
$b = new B;
$b->output_text(); //prints out static::$text value
?>
Post Reply