1

I am getting the following error and I can't seem to figure out why or how it's getting triggered.

Fatal error: Cannot access empty property in /home/content/p/l/a/plai1870/html/com/php/Bone/Compiler.php on line 18

Line 18 is

throw new LogicException($this->$compilers[$language]." is not a supported compiler.");

Here is Compiler.php

<?php
namespace Bone;
use LogicException;

class Compiler implements \Bone\Interfaces\Compiler {

    protected $compiler;

    protected $compilers = array(
        "php"           => "PHP",
        "as3"           => "ActionScript3",
        "javascript"    => "Javascript"
    );

    public function __construct($language) {
        $language = strtolower($language);
        if (!isset($this->$compilers[$language])) {
            throw new LogicException($this->$compilers[$language]." is not a supported compiler.");
        }
        $compiler = "\Bone\Compilers\\".$this->$compilers[$language]."\Compiler";
        $this->compiler = new $compiler();
    }

    public function buildDefinition($object, $path = null) {
        return $this->compiler()->buildInterface($object, $path);
    }

    public function buildObject($object, $path = null) {
        return $this->compiler->buildObject($object, $path);
    }   

    public function parameters($method) {
        return;
    }

    public function save($data, $path) {
        return;
    }
}
?>

EDIT And I'm calling it with:

$compiler = new \Bone\Compiler("php");
2
  • 2
    $this->$compilers ---> $this->compilers - remove the $ after ->. Commented Nov 7, 2012 at 16:31
  • Ugh I wish I could downvote PHP for those backslashes Commented Nov 7, 2012 at 18:19

2 Answers 2

7

Sorry if this is the most obvious, but:

throw new LogicException($this->$compilers[$language]." is not a supported compiler.");

As it has been checked that the property does not exists, shouldn't it be:

throw new LogicException("$language is not a supported compiler.");

?

Edit:

$this->$compilers[$language]
       ^- variable property

Remove the $ there:

$this->compilers[$language]

Then you can check if the entry in the array is set, not if a propery with the name of the value inside the (unset) array $compilers (local variable) is set.

When developing, always switch on warnings and notices (highest error level you can imagine) to not run into these problems without having PHP warn you first.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, if you see my edit, you will see that it does exist. Compiler::compilers has a key 'php' so it should not be throwing the error. $this->compilers does exist if you look at the source!
But I do see the error there. But I also get it on the next line when it sets $compiler = "\Bone\Compilers\\".$this->$compilers[$language]."\Compiler"; after checking if the key exists.
What you do not understand is the one $ too much. the local variable $compilers is not defined. The property is defined (it has the same name), however you are not accessing it there. Try with $this->compilers[$language] instead.
Wow, now I feel like an idiot. I think it's about time for some sleep!
I can not judge about your sleep, but this is some error everybody does at some time ;)
3

Your array is $this->compilers, not $this->$compilers.

$compilers doesn't exist in your function, so $this->$compilers was looking for a blank property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.