Code behind pattern

Hi,

I just started using PhpStorm because I was missing type checking in PhP and I want to use PhpStorm to simulate it using Phpdoc. Now I have already encountered a problem.

For each page there is a php file containing logic part (page.php) and a template file for design (page.phtml). The logic part is build in OO, so it is an object that uses include to include the design (page.phtml). The problem occurs in page.phtml then i try to call methods of the OO owning <?php echo $this->GetHeader(); ?>, of course it does not recognize the method GetHeader(), because it only sees itself as $this, it has no reference to the environment it is included in. Is there any way to specify in the page.phtml which object instance should be considered as $this.

7 comments
Comment actions Permalink

Just to make sure, everything works fine runtime, the only problem is in PhpStorm recognizing methods from the object it is included from.

And each logic page and design page has a 1:1 relationship, no template can be used by different objects, it would of course make it impossible.

0
Comment actions Permalink

Hi Ulrik,

Is there any way to specify in the page.phtml which object instance should be considered as $this.


Use PHPDoc comment on top of your template -- should help:

/** @var $this MyControllerClass */

0
Comment actions Permalink

Thanks, it solves most of my problems. And sorry for the late response.

The only problem is that it does not inherit the require_once ... in the top of my php file, I could ofcourse also include them in the phtml file, but it would result in double maintaince, any solution?

0
Comment actions Permalink

Hi,

AFAIK no solution available for this yet. But if you can describe what those "duplicate require_once" are for (what benefits you are having vs what problems you are facing now) and provide some basic example, then maybe I will advice something.

0
Comment actions Permalink

It is just some includes with some global functions I am missing some times in php. But I have some workarounds myself:

1. Add all the includes as libraries, the methods should always be available then, the only problem is if I use some functions which I have not choosen to include in this php.
2. Wrap the global functions in an statc class, which has the same problem, but writing like DateClass::date_to_relative_string(), should be able to resolve DateClass without including it by specifying it in phpdoc.


By the way, are there a way to include filename in phpdoc you wrote, in case of duplicate class names?

0
Comment actions Permalink

All functions should be available everywhere, even without include/require statements (same as classes). You have to use include/require so that PHP interpreter can find them during runtime, but during editing with PhpStorm it is not required -- IDE should pick them up as long as it is placed anywhere in the project (or added as External Library in "Settings | PHP | Include paths"). Never had any issues in this regard.

By the way, are there a way to include filename in phpdoc you wrote, in case of duplicate class names?

No -- all class names should be unique, otherwise you will see different kind of errors (like "Undefined method" etc).

There should be no problem with creating unique class names:

  1. Use namespaces (PHP 5.3)
  2. Use prefixes in class names, e.g. msfDate (msf = My Simple Framework) .. or My_Date etc
0
Comment actions Permalink

You are correct, did not notice it.

All my class names are unique, I was just wondering :)

Thanks for the help.

0

Please sign in to leave a comment.