can i call method in a class similar to $this->myFunction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christopher vogt

    can i call method in a class similar to $this->myFunction

    Hi,

    i'm wondering if there is something like $this-> to call a method inside
    another method of the same class without using the classname in front.

    I actually use

    class TEST
    {
    function func1()
    {
    }
    function func2()
    {
    TEST::func1();
    }
    }

    is there something i can use instead of TEST so that i don't need to change
    every line when i rename the class?

    Thx in advance,

    Chris


  • powerboy

    #2
    Re: can i call method in a class similar to $this->myFuncti on

    "christophe r vogt" <christopher.vo gt@rwth-aachen.de> wrote in message
    news:bivv28$fg4 $1@nets3.rz.RWT H-Aachen.DE...[color=blue]
    > Hi,
    >
    > i'm wondering if there is something like $this-> to call a method[/color]
    inside[color=blue]
    > another method of the same class without using the classname in[/color]
    front.[color=blue]
    >
    > I actually use
    >
    > class TEST
    > {
    > function func1()
    > {
    > }
    > function func2()
    > {
    > TEST::func1();
    > }
    > }
    >
    > is there something i can use instead of TEST so that i don't need to[/color]
    change[color=blue]
    > every line when i rename the class?[/color]


    $this->func1() inside of func2 will work.


    Comment

    • christopher vogt

      #3
      Re: can i call method in a class similar to $this-&gt;myFuncti on

      > $this->func1() inside of func2 will work.

      unfortunately it does only work if i create an object of class TEST. But i
      want to access the methods without creating an object:

      <?php
      TEST::func2();

      class TEST
      {
      function func1()
      {
      }
      function func2()
      {
      $this->func1();
      }
      }
      ?>

      This code leads to:
      Fatal error: Call to a member function on a non-object in
      C:\wampp2\htdoc s\Sites\ITS\TMP 3jxyokjte1.php on line 11

      So what can i do instead of using TEST::func1(); because i don't want to use
      the name of the class more than (after the word "class") once in the
      declaration of the class.

      Thx,
      chris


      Comment

      • christopher vogt

        #4
        Re: can i call method in a class similar to $this-&gt;myFuncti on

        Someone told me the answer...

        i was searching for self:: which is noch implemented in PHP yet but will be
        sometime.

        Chris


        Comment

        • CC Zona

          #5
          Re: can i call method in a class similar to $this-&gt;myFuncti on

          In article <bj04b3$l45$1@n ets3.rz.RWTH-Aachen.DE>,
          "christophe r vogt" <christopher.vo gt@rwth-aachen.de> wrote:
          [color=blue][color=green]
          > > $this->func1() inside of func2 will work.[/color]
          >
          > unfortunately it does only work if i create an object of class TEST. But i
          > want to access the methods without creating an object:
          >
          > <?php
          > TEST::func2();
          >
          > class TEST
          > {
          > function func1()
          > {
          > }
          > function func2()
          > {
          > $this->func1();
          > }
          > }
          > ?>
          >
          > This code leads to:
          > Fatal error: Call to a member function on a non-object in
          > C:\wampp2\htdoc s\Sites\ITS\TMP 3jxyokjte1.php on line 11[/color]

          The reason that example fails is because $this is not a valid reference
          inside a method called with the :: operator. From <http://php.net/manual/keyword.paamayi m-nekudotayim.php >:

          "There are class functions, but there are no class variables. In fact,
          there is no object at all at the time of the call. Thus, *a class function
          may not use any object variables (but it can use local and global
          variables), and it may no use $this at all*." (Emphasis added)

          Thus a call to TEST::func1() works, but TEST::func2() throws a fatal error.
          [color=blue]
          > So what can i do instead of using TEST::func1(); because i don't want to use
          > the name of the class more than (after the word "class") once in the
          > declaration of the class.[/color]

          AFAIK, there is no such option. If you want to call a class method without
          creating an object, you must call the class by name.

          It sounds as though wrapping this function in a class might not be useful
          for you in this circumstance. Have you considered a different design
          approach? Maybe what you'd be better off declaring func2() as a non-class
          function.

          --
          CC

          Comment

          • christopher vogt

            #6
            Re: can i call method in a class similar to $this-&gt;myFuncti on

            > > This code leads to:[color=blue][color=green]
            > > Fatal error: Call to a member function on a non-object in
            > > C:\wampp2\htdoc s\Sites\ITS\TMP 3jxyokjte1.php on line 11[/color][/color]
            SURE ;), my fault, postet the wrong code in my second posting, the first one
            has the right code....
            [color=blue]
            > "There are class functions, but there are no class variables. In fact,
            > there is no object at all at the time of the call. Thus, *a class function
            > may not use any object variables (but it can use local and global
            > variables), and it may no use $this at all*." (Emphasis added)[/color]

            got this and that were where my question took place. I was searching for the
            missing self:: abstraction (someone told me) but it will be added in PHP 5.
            [color=blue]
            > Thus a call to TEST::func1() works, but TEST::func2() throws a fatal[/color]
            error.
            sure, because of $this->...
            [color=blue][color=green]
            > > So what can i do instead of using TEST::func1(); because i don't want to[/color][/color]
            use[color=blue][color=green]
            > > the name of the class more than (after the word "class") once in the
            > > declaration of the class.[/color]
            > AFAIK, there is no such option. If you want to call a class method[/color]
            without[color=blue]
            > creating an object, you must call the class by name.[/color]
            Yes but it seems a bit strange that you HAVE to use the classname INSIDE the
            class, but that will be fixed, as i said before
            [color=blue]
            > It sounds as though wrapping this function in a class might not be useful
            > for you in this circumstance. Have you considered a different design
            > approach? Maybe what you'd be better off declaring func2() as a non-class
            > function.[/color]
            Thats not possible (in a fine way) for my case because the class offer a
            simple database-abstraction and -interface.

            But thanks for your Posting :).

            Chris


            Comment

            Working...