5

maybe it is to early in the morning or I'm totally blind, but why to I get a 'Fatal error: Using $this when not in object context' in the following code. There is nothing static in there.

The class:

<?php

class Property
{

    /**
     * @var string[]
     */
    private $values;

    public function __contruct($file)
    {
        $this->values = parse_ini_file($file, false);
    }

    /**
     * @return string
     */
    public function get($key, $default = null)
    {
        if (key_exists($key, $this->values)) { //<-- error
            return $this->values[$key];
        }
        return $default;
    }
}

The test:

<?php

class PropertyTest extends Test
{

    public function testGet()
    {
        $prop = new Property($this->getResource('test.properties'));
        $this->assertEquals('foo', $prop->get('test.entry', 'xyz')); 
        $this->assertEquals('bar', $prop->get('test.entry2', 'xyz'));
        $this->assertEquals('xyz', $prop->get('test.entry3', 'xyz'));
        $this->assertEquals(null, $prop->get('test.entry3'));
    }
}

Edit

The error comments indicating the trace. The error occures while running the PropertyTest->testGet() on the the first $prop->get() caused by the first line of the Property->get() method.

Solution

Beside the typo xdazz found, and the deprecation Phil pointed at, user985935 was right. :)

To make it short, my class loader used the static get and the phpUnit error mislead my investigations and my information I offer you for finding my problem. Sorry.

mheinzerling
  • 1,035
  • 1
  • 10
  • 31
  • 1
    Shouldn't that be `array_key_exists()`? – Phil Nov 09 '11 at 05:30
  • Just testing, seems that `key_exists()` is a valid function, at least in 5.3.6. Also, cannot replicate this error – Phil Nov 09 '11 at 05:37
  • 1
    What does the full error message say, including file and line references? – Phil Nov 09 '11 at 05:45
  • 1
    @Phil For backward compatibility, the following deprecated alias may be used: key_exists() – xdazz Nov 09 '11 at 05:53
  • I added the explanation to my line references. It's very strange since the constructor is working, and the $prop->get() is definitely not static. – mheinzerling Nov 09 '11 at 05:57

2 Answers2

7

There is a typo in your code.

public function __contruct($file)

which should be

public function __construct($file)
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Thanks. I'm currently at work, and can't retest it, but is seem very likely that this could cause the problem. So it was really to early in the morning ;) – mheinzerling Nov 09 '11 at 06:02
-4

'Fatal error: Using $this when not in object context'

The $this keyword points to methods that are inside an object only you can't use $this outside an object I think this is what causing your problem

Christopher Pelayo
  • 792
  • 11
  • 30
  • This is the error: 'Fatal error: Using $this when not in object context' so how would you analyze the error returned then? – Christopher Pelayo Nov 12 '11 at 01:52
  • @BoltClock if you would analyze the error and don't just based it on the code that the user posted here that is the thought of the problem we may not see it directly to what was posted but it might be happening somewhere else on the application I have already experienced the same problem to several developments I've had and I have encountered this error many times already. ;) – Christopher Pelayo Nov 12 '11 at 02:05
  • @daGrevis it's just a matter of how we should able to analyze things. hehehe. ;) – Christopher Pelayo Nov 12 '11 at 09:31