NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Code completion on "$this" in an included file


Joined: 19 Aug 2006
Posts: 76
Reply with quote
Let's say:

Code:

class MyClass
{
    public function test()
    {
        include('inc.php');
    }

    public function sayHi()
    {
        echo "hi";
    }


}


and, in "inc.php":

Code:


/**
* @var MyClass
*/
$this->



I'd like phpEd to suggest me "sayHi()" here, but it doesn't. The "@var MyClass" doesn't seem to work on "$this".

Is there a way I can let phpEd knows the type of "$this" in an included file?
View user's profileFind all posts by jlSend private message


Joined: 15 Dec 2010
Posts: 5
Reply with quote
i don't get what you are trying to do with $this..




View user's profileFind all posts by willbakerSend private message


Joined: 19 Aug 2006
Posts: 76
Reply with quote
I think my message is pretty clear willbaker..
View user's profileFind all posts by jlSend private message
Guru master

Joined: 05 Jul 2004
Posts: 659
Location: Belgium
Reply with quote
Actually I agree with willbaker... How can you have a "$this" in an include file that is supposed to be a class defined in another file? Unless you are using $this as a regular variable and that's never a good idea... perhaps you can share a bit more code?
View user's profileFind all posts by BlizzSend private messageVisit poster's website


Joined: 19 Aug 2006
Posts: 76
Reply with quote
Well, I'll try to be more clear then... Embarassed

Template.php
Code:


class Template
{
  $name = "";
  public function echosafe($str)
  {
    echo htmlentities($str, ENT_QUOTES, 'UTF-8');
  }
 
  public function run($name)
  {
    $this->name = $name;
    include("myTemplate.tpl.php");
  }
}



myTemplate.tpl.php
Code:

<html>

  <head>
  </head>
 
  <body>
    <?php $this->echosafe($this->name) ?>
  </body>

</html>


an finally, let's say my code is ran with:
Code:


$template = new Template();
$template->run("John Doe");


--------------
My problem:

In "myTemplate.tpl.php" I'd like to let phpEd know that "$this" is an instance of the "Template" class, so phpEd can suggest me "echosafe()" when I type <?php $this->

I tried to let it know using the "@var Template" syntax, just above a $this variable in "myTemplate.tpl.php", but it doesn't seem to work!
View user's profileFind all posts by jlSend private message
Guru master

Joined: 05 Jul 2004
Posts: 659
Location: Belgium
Reply with quote
Hmm that makes a lot more sense. And are you sure you did like
/** @var Template */
and not
/* @var Template */
by mistake?

PhpED only parses blocks if they start with 2 asterisks.
View user's profileFind all posts by BlizzSend private messageVisit poster's website


Joined: 19 Aug 2006
Posts: 76
Reply with quote
Yes, I did use "/** @var Template */" ,

This syntax always work, except for "$this".
View user's profileFind all posts by jlSend private message
Guru master

Joined: 05 Jul 2004
Posts: 659
Location: Belgium
Reply with quote
Guess PhpED doesn't allow override on $this then, although it probably should if it can't determine the type itself.
View user's profileFind all posts by BlizzSend private messageVisit poster's website
Guru master

Joined: 24 Jul 2009
Posts: 737
Reply with quote
PhpED quite correctly always expects $this to represent the current class context and you can't reassign that. Because that template isn't within a class definition, I don't think you will get code completion to work on $this at coding time, although at run-time with debug, PhpED will be able to see the context of $this from the debugger.

However, this works:
Code:
class Template {
  public function hello() {
    return 'value';
  }
}

/**
* @var Template
*/
$_this = $this;
$_this->;

Place your cursor after $_this-> and you get code completion. Because $_this is an assignment of $this (rather than a copy or clone), the new object works as expected.
View user's profileFind all posts by plugnplaySend private message
Code completion on "$this" in an included file
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic