how do i avoid typing the keyword "$this->" every time inside a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph S.

    how do i avoid typing the keyword "$this->" every time inside a class

    Hi all,
    how do I avoid typing the keyword "$this->" every time I need to
    reference a member of a class inside the class?

    (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
    find it a bit annoying)

    TIA,
    JS

  • Miguel Cruz

    #2
    Re: how do i avoid typing the keyword "$this->" every time inside a class

    "Joseph S." <js_dev@rediffm ail.comwrote:
    how do I avoid typing the keyword "$this->" every time I need to
    reference a member of a class inside the class?
    Set up a function key macro in your editor?

    miguel
    --
    Photos from 40 countries on 5 continents: http://travel.u.nu
    Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
    Airports of the world: http://airport.u.nu

    Comment

    • mootmail-googlegroups@yahoo.com

      #3
      Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class

      Joseph S. wrote:
      Hi all,
      how do I avoid typing the keyword "$this->" every time I need to
      reference a member of a class inside the class?
      >
      (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
      find it a bit annoying)
      >
      TIA,
      JS
      For what it's worth, I use Zend studio, and with it's autocomplete,
      typing $t is usually enough to get $this as the first entry in most
      cases.

      I agree, though, that it can get a bit tedious after a while in any
      decent sized class.

      Comment

      • Kenneth Downs

        #4
        Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class

        Joseph S. wrote:
        Hi all,
        how do I avoid typing the keyword "$this->" every time I need to
        reference a member of a class inside the class?
        >
        (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
        find it a bit annoying)
        >
        back when the world was young, in foxpro we would do this:

        WITH THIS
        .property1 = 'value'
        .property2 = 'value'

        ENDWITH

        AFAIK this language element does not exist in PHP.

        --
        Kenneth Downs
        Secure Data Software, Inc.
        (Ken)nneth@(Sec )ure(Dat)a(.com )

        Comment

        • Jerry Stuckle

          #5
          Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time insidea class

          Joseph S. wrote:
          Hi all,
          how do I avoid typing the keyword "$this->" every time I need to
          reference a member of a class inside the class?
          >
          (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
          find it a bit annoying)
          >
          TIA,
          JS
          >
          Unfortunately, you can't.

          $this->foo is a class variable.

          $foo is a local variable.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • s a n j a y

            #6
            Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time insidea class

            Joseph S. wrote:
            Hi all,
            how do I avoid typing the keyword "$this->" every time I need to
            reference a member of a class inside the class?
            >
            (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
            find it a bit annoying)
            >
            TIA,
            JS
            >
            I usually copy paste it, or use those column select (aka block select)
            feature of good editors for multiple lines. Edit plus comes to mind.

            Comment

            • dawnerd

              #7
              Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class


              s a n j a y wrote:
              Joseph S. wrote:
              Hi all,
              how do I avoid typing the keyword "$this->" every time I need to
              reference a member of a class inside the class?

              (coming from a world of cozy auto-complete enabled Java / .Net IDEs I
              find it a bit annoying)

              TIA,
              JS
              >
              I usually copy paste it, or use those column select (aka block select)
              feature of good editors for multiple lines. Edit plus comes to mind.
              I usually just type it, it is just a few extra characters. It doesn't
              take up that much time, unless you type very slowly...

              Comment

              • william.clarke

                #8
                Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class

                Why not get an editor with autocomplete... have a look at PHPed, Zend
                Studio and lots of the other IDEs out there.
                When I started doing Java I did it in the "cosy" Notepad IDE with a
                command line JDK compiler, so strangely autocomplete didn't work there
                either. If your tools aren't doing the job, find better tools instead
                of looking for shortcuts in the language ;)

                Comment

                • william.clarke

                  #9
                  Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class

                  Sorry should have posted links:

                  PhpED: http://www.nusphere.com/products/phped.htm
                  Zend: http://www.zend.com/products/zend_studio/feature_list

                  Obviously neither of these is free, but you could try Eclipse with a
                  Php add-on.

                  Comment

                  • Joseph S.

                    #10
                    Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class

                    Hi all,
                    first, thanks for all the replies.
                    (1)
                    >Unfortunatel y, you can't.
                    >$this->foo is a class variable.
                    >$foo is a local variable.
                    >============== ====
                    >Remove the "x" from my email address
                    >Jerry Stuckle
                    ok. Thinking of it, IMHO, this is a big excuse we (PHP community) give
                    to show minus points about PHP - Refer to


                    (2)
                    william.clarke wrote:
                    Sorry should have posted links:
                    PhpED: http://www.nusphere.com/products/phped.htm
                    Zend: http://www.zend.com/products/zend_studio/feature_list
                    Obviously neither of these is free, but you could try Eclipse with a Php add-on.
                    I am using PHPEclipse for a long time now, (but wrote all my code in
                    procedural style, not touching OOP) - but it pops up huge autocomplete
                    list, and somehow, no "$this" - i'll have to look at the configuration
                    - (thanks anyway)

                    (3)
                    My own personal opinion is as follows:
                    if not for the "$" and the "->", PHP would have _eaten_ up a lot more
                    of the market share - that it is already doing so is not surprising. It
                    is simply the easiest language to work in. Lots of flexibility,
                    plethora of well designed functions and a very pratical approach,
                    rather than the purist apporach of Java/.Net designers which result in
                    more code being required to be written to accomplish simple tasks -
                    most obvious examples are file() and file_get/put_contents().

                    A Google summer of code project promises something interesting PHP
                    Preprocessor Macros:


                    Finally, as a practical workaround, maybe a simple global function
                    could be written, say, function v($arg){
                    return (eval("\$this->".variable_nam e($arg));
                    }
                    I am looking for the function variable_name() . Looked around a bit, but
                    could not find one.

                    Or better still, one could write a slightly complex parsing script, say
                    code-cleaner.php, send original code to that script
                    (PHP in CLI mode:)
                    php -n code-cleaner.php myscript.php

                    which will do all the substitution and overwrite myscript.php with
                    "$this->" code.
                    I'll try this and post it if it succeeds.

                    One of the important things here is that PHP allows two $foo variables
                    in one class:
                    $foo = 5;
                    class A{
                    public $foo = 6;
                    public function showfoo(){
                    echo $foo; // 6
                    echo $this->foo; // 5
                    }
                    }
                    and that is why $this is required. I believe (please correct if wrong)
                    other languages give errors like "$foo is ambiguous". I dont know as
                    much of compiler internals as to know why $this-is required
                    _everywhere_.
                    This should have gone into PHP5, really, during the huge OOP rewrite.

                    Meanwhile,
                    people could have a look at

                    which shows how to get the name of a variable.

                    Regards,
                    JS.

                    Comment

                    • Richard Levasseur

                      #11
                      Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class


                      Joseph S. wrote:
                      (2)
                      william.clarke wrote:
                      Sorry should have posted links:
                      PhpED: http://www.nusphere.com/products/phped.htm
                      Zend: http://www.zend.com/products/zend_studio/feature_list
                      Obviously neither of these is free, but you could try Eclipse with a Php add-on.
                      I am using PHPEclipse for a long time now, (but wrote all my code in
                      procedural style, not touching OOP) - but it pops up huge autocomplete
                      list, and somehow, no "$this" - i'll have to look at the configuration
                      - (thanks anyway)
                      >
                      The PHP plugin for eclipse is mediocre at best. I find I fight it most
                      of the time:

                      1) New lines within quoted strings: it will auto end the line and make
                      it concatenate, but PHP will allow returns within the line, VERY
                      annoying when you're writing quoted HTML attributes within a HEREDOC
                      block.
                      2) Very poor autocomplete compared to other programs. $this-pops up
                      a huge list of potential matches. using built in functions will only
                      display the parameter list until you start the parentheses, in which
                      case it starts suggesting variables, which usually start with the super
                      globals. VERY frustrating.
                      3) Aggresive auto-formatting:
                      * Auto-inserts closing } after a { even if it is within a closed block
                      that it, itself, closed, creating a syntax error.
                      * The opening of a block with { will begin to suggest variables, and
                      pressing enter will auto-insert the first variable, which is usually
                      $_GET.
                      * Auto-complete of variables is iffy, too often has i tried to use
                      auto-complete, typed the first few characters, tab'd, and it only left
                      those characters instead of completing the variable name, even if it
                      was the only variable available.
                      4) Poor scope detection: it frequently suggests global variables that
                      aren't declared as global within the scope, thus would be inaccessible.
                      5) Poor auto-complete of auto-globals. Dreamweaver will display an
                      autocomplete for variables within $_SERVER, which is very handy. PHP
                      Eclipse won't.
                      6) No concept of current type of variable (If you've use vs.php, the
                      php plugin for the visual studio, you know what i mean - it can
                      somewhat accurately guess the type of a variable).
                      7) Poor help tips, see (2). You can document all your methods with
                      @param, @return, but you'll never see these comments from auto-complete
                      unless you manually hover your mouse over the method...defeat ing the
                      point of a context help as you type.
                      8) I could go on, but i think this is enough.

                      If you can get a hold of the visual studio IDE, try the vs.php plugin.
                      I find it vastly superior to eclipse's

                      (3)
                      My own personal opinion is as follows:
                      if not for the "$" and the "->", PHP would have _eaten_ up a lot more
                      of the market share - that it is already doing so is not surprising. It
                      is simply the easiest language to work in. Lots of flexibility,
                      plethora of well designed functions and a very pratical approach,
                      rather than the purist apporach of Java/.Net designers which result in
                      more code being required to be written to accomplish simple tasks -
                      most obvious examples are file() and file_get/put_contents().
                      >
                      Why? I don't see how $ and -has much effect on the market share.
                      A Google summer of code project promises something interesting PHP
                      Preprocessor Macros:

                      >
                      Finally, as a practical workaround, maybe a simple global function
                      could be written, say, function v($arg){
                      return (eval("\$this->".variable_nam e($arg));
                      }
                      I am looking for the function variable_name() . Looked around a bit, but
                      could not find one.
                      >
                      Or better still, one could write a slightly complex parsing script, say
                      code-cleaner.php, send original code to that script
                      (PHP in CLI mode:)
                      php -n code-cleaner.php myscript.php
                      >
                      which will do all the substitution and overwrite myscript.php with
                      "$this->" code.
                      I'll try this and post it if it succeeds.
                      >
                      One of the important things here is that PHP allows two $foo variables
                      in one class:
                      $foo = 5;
                      class A{
                      public $foo = 6;
                      public function showfoo(){
                      echo $foo; // 6
                      echo $this->foo; // 5
                      }
                      }
                      and that is why $this is required. I believe (please correct if wrong)
                      other languages give errors like "$foo is ambiguous". I dont know as
                      much of compiler internals as to know why $this-is required
                      _everywhere_.
                      In PHP you have to qualify class/instance variables with $this or
                      classname::, if it isn't, its considered a local variable, more or less
                      (exceptions being 'global $var' and super globals)
                      In most languages, there's a heirarchy of scope. In general, most
                      specific overrides least specific:

                      If we were to consider some sort of php/java/c++ hybrid:
                      $foo = '3';
                      class bar {
                      public $foo = '2';
                      function baz($foo='1';) {
                      $foo // local variable, 1
                      $this.$foo // instance, 2
                      $GLOBALS['foo'] // global variable, 3
                      }
                      }

                      in java, you can do:

                      class bar {
                      public name;
                      void myfunc(String name) {
                      name = "John"; // refers to local name
                      this.name = "Mary"; // refers to instance name
                      }
                      }

                      Most languages have that sort of heirarchy, anyways.

                      And in response to original poster: Just use $this->, its not that much
                      of a hassle.
                      If you dont' like typing $this-before every var, just do $myvar = &
                      $this->myvar; at the start of the method.

                      Comment

                      • mootmail-googlegroups@yahoo.com

                        #12
                        Re: how do i avoid typing the keyword &quot;$this-&gt;&quot; every time inside a class


                        Richard Levasseur wrote:
                        >
                        Why? I don't see how $ and -has much effect on the market share.
                        I don't think it really does, either, but I just have to question the
                        wisdom of choosing the "->" over something like, say ".", which as you
                        said later in your post, some other languages have.

                        A "." is quick, simple, one keystroke, and my finger is right near that
                        key anyway.
                        A "->", on the other hand, requires me to move a finger to the numeric
                        row, then hold shift and hit another key (3 keystrokes vs 1).

                        As you mentioned, many other languages require scoping of class
                        variables vs local variables via a 'this', I think PHP just compounds
                        the issue with its choice of "->".

                        Yes, it's nice that it looks like an arrow and all, but can we leave
                        the ASCII art to the kiddies and instead make a language that favors
                        speed in writeability?

                        Comment

                        Working...