Jump to content

[SOLVED] LSB (PHP 5.3) problem with static value!


keevitaja

Recommended Posts

hello,

 

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

 
<?php
<?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
?>

Link to comment
Share on other sites

Ahh missed the 5.3 part sorry.

 

Here is the problem, you are using a parent method, and expecting it to use a static variable from a child class. This will not work simply because of scope. The parent has no access to the childs attributes, even its static ones.

 

However, you may want to take this into consideration:

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). 

 

I'm pretty sure you can't qualify the scope with the static key word. In fact, that doesn't even really make sense it all.

 

by the way, when I change your code to A:: i get the following output

 

first: first
second: second,
static text:
here shoult be some static text: 
here shoult be some static text: 
here shoult be some static text: 

 

Link to comment
Share on other sites

A:: will not work..

 

the actual code i'm trying to create is the model for working with sql database where $text is actually a database name. class A is the parent class and i will create a class for each sql table> B, C and so on! in parent i will do the basic stuff like add, delete etc.

 

it is possible to get the value into this output_text method?

Link to comment
Share on other sites

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

 

In your A::init() method, you create a new instance of "self" (in other words A) and return it. So, $b has an instance of the class A. When $b->ouput_text() is called, static refers to the class to which it belongs (A).  When B::output_text() is called, ditto (B). You then make $b an instance of class B, so $b->output_text() uses static pointing to class B. 

 

When you say "doesn't print out the static::text value", it actually does; just that the value is NULL (so outputs an empty string).

 

If I can understand your intentions correctly, your A::init() method should be doing $obj = new static;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.