# Weird behavior for $this variable in PHP4

- Thread: https://externals.io/message/3368
- Original archive: https://news-web.php.net/php.internals/3368
- Started by Josh Fuhs on 2003-07-11T21:38:32+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3368 — Josh Fuhs — 2003-07-11T21:38:32+00:00

URL: https://externals.io/message/3368#3368

Hello,

I'm getting a problem with $this being defined where it shouldn't be. The code
that I'm writing is a little weird in order to compensate for the lack of some
OO features in PHP4.

The PHP script at the bottom should give the behavior I'm referring to.

Is PHP5 in a more-or-less stable state? Are PHP4 extensions completely
compatible with PHP5?

Josh

<?php

/* No static attributes in PHP4. */
$static_class1=NULL;

class Class1{
    var $var1;
    function Class1(){ $this->var1=0; }

    function init(){
        global $static_class1;
	$static_class1 = new Class1();
    }

    function getVar(){
        /* Check for $this. This is a way of making a static method 
           and a regular method with the same name. */
        /* $this is defined here directly after called from the
           Class2 constructor. */
        if(isset($this)){
            return $this->var1;
	} else {
	    global $static_class1;
	    $static_class1->getVar();
	}
    }
}

class Class2{
    function Class2(){
        $a=Class1::getVar();
    }
}

Class1::init();
$a=new Class2();

?

---

## #3373 — Alan Knowles — 2003-07-12T00:54:25+00:00

URL: https://externals.io/message/3368#3373 · In reply to: #3368

in php4 $this gets passed in All static method calls ,as if they where 
parent::sss() - so statically calling one object method from another 
object instance will take '$this' with it..

Last word on this 'feature/bug' is that it will be removed in PHP5, (bit 
of a bugger - I'm sure some of my old code relied on it ;)

Regards
Alan


Josh Fuhs wrote:
> Hello,
> 
> I'm getting a problem with $this being defined where it shouldn't be. The code
> that I'm writing is a little weird in order to compensate for the lack of some
> OO features in PHP4.
> 
> The PHP script at the bottom should give the behavior I'm referring to.
> 
> Is PHP5 in a more-or-less stable state? Are PHP4 extensions completely
> compatible with PHP5?
> 
> Josh
> 
> <?php
> 
> /* No static attributes in PHP4. */
> $static_class1=NULL;
> 
> class Class1{
>     var $var1;
>     function Class1(){ $this->var1=0; }
> 
>     function init(){
>         global $static_class1;
> 	$static_class1 = new Class1();
>     }
> 
>     function getVar(){
>         /* Check for $this. This is a way of making a static method 
>            and a regular method with the same name. */
>         /* $this is defined here directly after called from the
>            Class2 constructor. */
>         if(isset($this)){
>             return $this->var1;
> 	} else {
> 	    global $static_class1;
> 	    $static_class1->getVar();
> 	}
>     }
> }
> 
> class Class2{
>     function Class2(){
>         $a=Class1::getVar();
>     }
> }
> 
> Class1::init();
> $a=new Class2();
> 
> ?>
> 
> 


-- 
Can you help out?
Need Consulting Services or Know of a Job?
http://www.akbkhome.com
