By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
> $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

P: n/a
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

P: n/a
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

P: n/a
> > 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.