How can external classes like from PEAR referenced

I'm using Savant3 for templating and don't want to include it into my project code, so would like to to define the class for proper recognition.

 public function showMirrorUserResults($par ) {
        global $KI_tpl;
        $tpl            = new Savant3($KI_tpl);
         $returnRequest  = 'status-mu';
         $databucket  = KI_DATA_BUCKET;

the class Savant3 will be colored as it isn't unknown and all additional varaiables are colore as well

        $tpl->hasFileGraph      = true;
        $tpl->sampleFileList1   = json_encode($this->normalizeFileGraphList($fileList));

Any ideas how I could get the class defined so it s properly recognized throughout its usage?

Thanks

8 comments
Comment actions Permalink

Hi Anton,

1) File | Settings | PHP
2) Add the path to your library as an "Include Path"

Now that library will be listed in Project view under External Libraries branch and can be used for code completion/resolution.

0
Comment actions Permalink

Dear Andriy,

Thanks for the tip, I'm afraid however I didn't explain my situation well enough as my environment is more complicated.

I'm using a Mac with 2 VM one running Windows that acts as an engine for special tasks and the second is a Linux VM that runs the application that I'm developping in the Mac and can feed multiple window VM's with tasks. The real implemententations is done with EC2 VM on Amazon's platform. So my Mac is the development system and the VM's on the MAc are a test platform.

My flow is as follows I develop on the Mac have a shared directory at a certain level to the VM Linux. The mac doesn't even have PHP (maybe stupid and maybe I must change that).

When the sw is ready for testing, I move to changed classes etc to the VM Linux (nice PhpStorm function for that) and run my local test, if OK the changes go into a SVN.
My application has an SVN installer implemented so as administrator I can pull an update or a complete new version into the test system or the production system
So the Linux VM's have the PEAR directory which is not directly sharable  

As I'm only using very limited methods from the class as Savan3 bases its logic on PHP I thaught I could simply define some PHPDoc or whatever definition to get the class and methods used defined.

I'm open to any approach that reduces the amount of updating of the different systems.

Thanks

0
Comment actions Permalink

Hi Anton,

Yes, you can do it by creating a stub file (all classes that are already supported by PhpStorm are done in this way):
* create a file (or use existing one) where you going to have your class definitions and PHPDoc for Savan3
* declare all required classes/constants/fields/methods/functions with appropriate PHPDoc comments
* include this file somehow into your project (via External libraries, for example)

Example of such implementation:

<?php
class someClass
{
    /** Some important constant */
    const SOME_VAL = 1;


    /** @var string Very important variable */
    public $var = 'Hello';

    /**
     * This function does something
     *
     * @param mixed $param1 Very important parameter
     * @param int $param2 Optional parameter
     * @return string
     */
    public function someFunc($param1, $param2 = 12) { }

    /**
     * This is another Func that does something
     *
     * @return anotherClass
     */
    public function anotherFunc() { }
}

The class above will have 2 methods,1 variable and 1 constant -- all with documentation.

Generally speaking in your case it will be limited and stripped down version of the Savan3 (all function/method bodies are empty here and you can only declare the methods/fields you are going to use).

P.S.
Why don't you just copy this Savan3 somewhere on your Mac (any location will do, PHP is not required as well)? As long as this library is not getting updated very often (like every day) then you will be fine with connecting it as an External Library.

0
Comment actions Permalink

Hi Andriy,

I copied the Savant3.php into a library directory and yes the class is now being recognized, thanks again. However I must be doing something wrong.


As the below still report $tpl->hasFileGraph as undefined field and ofcourse al the others that I did not define. The whole trick of Savan3 is that you add all your variables as fields to the object and pull them off from $this in the template       

if ($fileList != false)
        {
        /** @var $hasFileGraph */    
            $tpl->hasFileGraph      = true;
            $tpl->sampleFileList1   = json_encode($this->normalizeFileGraphList($fileList));

            if($queueList != false){

                $tpl->sampleFileList2   = json_encode($this->normalizeFileGraphList($queueList));
            }

            $tpl->userGraph         = $tpl->fetch('userGraph.tpl.php');
        }



Also in the template all fields are reported as undefined when I define $this, otherwise only the first $this is highlighted but do I mis the checking for real unknown fields

/** @var $this */
or
/** @var Savant3 $this*/

Any insights are welcomed
0
Comment actions Permalink

Hi Anton,

If you want to define dynamic class fields (PHP uses magic  __get() and __set()  methods to access such fields and their values during runtime) then the only way ATM to do this is via @property:

<?php
/**
* @property int $someField Some very special dynamic class variable
*/
class someClass
{
    /**
     * This is another Func that does something
     */
    public function anotherFunc()
    {
        $this->someField = 13;
    }
}


I do not think (99.99% I would say) that it is possible to re-declare $this inside the class.

But yes -- you can define what $this is outside the class (like inside template file, for example):

<?php
/** @var someClass $this */
$this->someField;

0
Comment actions Permalink

Hi Andriy,

I checked a number of combinations as your example works but my usage is different

<?php

class someClass
{
    /**
     * This is another Func that does something
     *
     * @return anotherClass
     */

    public function anotherFunc()
    {
        $tpl = new Savant3();
        $tpl->someField = true;
    }
}


The above only works when I put

    /**

    *@property boolean $someField Some very special dynamic class variable

    */

in the Savant3.php file and above the class definition and it will then recognize the field in the template as well with

/** @var Savant3 $this */ documentation in the template

However think about it how complicated that gets when for every GUI usage I have to extend the Savant3 file. That completely defeats the purpose of the approach and then any field that is defined before the Savant3 class will pass the criteria for every template so still no real checking. If there is some way to specify the definition in the file where the someclass resides it would be clean, so that only the defined fields in the calling class are part of the template definition. If we can get to that approach we have a useable solution.

Thanks anyway for all the good advise

0
Comment actions Permalink

After some additional testing I have moved away from using the std object, but in stead use an object with one array, so that I only have to define one parameter and add that to the Savant3.php file.

so   
    

   /**

    * @property array $par

    */


If needed I can do the array keys through variables so that they can be copied for the template as well and the IDE can help me avoid stupid typos. This probably is even better than using stnd objects unless what is defined in the calling class can checked by the IDE when working on the template


     $tpl                   = new Savant3();
     $tpl->par['title']     = "my title';


or

     $title                 = "title";
     $tpl                   = new Savant3();
     $tpl->par[$title]      = "my title";


Do you see any improvements?

0
Comment actions Permalink

Hi Anton,

If there is some way to specify the definition in the file where the someclass resides it would be clean, so that only the defined fields in the calling class are part of the template definition.

No, it is not possible ATM and I do not think that devs will be implementing something like this in near or distant future (such approach/support in IDE will be convenient, no doubts -- but is not persistent/clean solution as you will have a situation when class properties defined in different/multiple locations + it will be seen by IDE as the same named class but with completely different set of properties depending on calling class).

If you wish -- you may submit a feature request on Issue Tracker -- maybe you know how it is implemented/supported in other IDEs (for example -- how others dealing with Zend_Cache (from Zend Framework) which uses the same dynamic fields approach?). I personally very skeptical about success of such request.


The idea/approach with array will be the best solution at the current IDE state.

0

Please sign in to leave a comment.