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