Page 1 of 1
LSB (PHP 5.3) problem with static value!
Posted: Fri Oct 30, 2009 11:24 am
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
?>
Re: LSB (PHP 5.3) problem with static value!
Posted: Fri Oct 30, 2009 12:09 pm
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;
}
Re: LSB (PHP 5.3) problem with static value!
Posted: Fri Oct 30, 2009 12:27 pm
by keevitaja
i solved the problem:
Code: Select all
public static function init($one = '', $two = '') {
$obj = new static;
Re: LSB (PHP 5.3) problem with static value!
Posted: Fri Oct 30, 2009 12:40 pm
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
?>