468,117 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

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
Jul 16 '05 #1
5 9700
"christopher vogt" <ch**************@rwth-aachen.de> wrote in message
news:bi**********@nets3.rz.RWTH-Aachen.DE...
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?

$this->func1() inside of func2 will work.
Jul 16 '05 #2
> $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\htdocs\Sites\ITS\TMP3jxyokjte1.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
Jul 16 '05 #3
Someone told me the answer...

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

Chris
Jul 16 '05 #4
In article <bj**********@nets3.rz.RWTH-Aachen.DE>,
"christopher vogt" <ch**************@rwth-aachen.de> wrote:
$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\htdocs\Sites\ITS\TMP3jxyokjte1.php on line 11


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.paamayim-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.
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.


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
Jul 16 '05 #5
> > This code leads to:
Fatal error: Call to a member function on a non-object in
C:\wampp2\htdocs\Sites\ITS\TMP3jxyokjte1.php on line 11 SURE ;), my fault, postet the wrong code in my second posting, the first one
has the right code....
"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)
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.
Thus a call to TEST::func1() works, but TEST::func2() throws a fatal error.
sure, because of $this->...
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.

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. 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
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.

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
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by kackson | last post: by
2 posts views Thread by JS | last post: by
2 posts views Thread by cmay | last post: by
27 posts views Thread by Jason Shohet | last post: by
reply views Thread by Pawan Narula via DotNetMonster.com | last post: by
2 posts views Thread by Kimmo Laine | last post: by
1 post views Thread by masterofthebus | last post: by
13 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.