473,246 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 10020
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kackson | last post by:
Hi. What does it mean when it is written as: in the test.hpp file-> class MYFUNCTION: public TOP { public: ....some variables... MYFUNCTION(int x, int y, int z.. and some more argument);
2
by: JS | last post by:
I read the following in som sample code: function createOpts(sel){ var s = new Array(); var num = 0, txt = "bo"; var ar = opt = null; var n = sel.selectedIndex; var args =...
2
by: cmay | last post by:
I am having a problem with the use of "this". Maybe someone can help me out here, let me know if I am doing this wrong: Lets say I have an object MyObject, which has local variables, and some...
0
by: BUX | last post by:
I have myDB with myForm1 and myForm2. MyForm1' s code call myFunction() in a library database. MyFunction have to call myfForm2_AfterUpdate MyfForm2_AfterUpdate is declared as public. ...
27
by: Jason Shohet | last post by:
I have an ascx control, and it needs a value called 'orgID' which the parent page will have. I want to avoid the parent page setting values in fields on the ascx control since this violates...
1
by: Tom | last post by:
I am dynamically loading a DLL that I wrote that contains forms, a module, and classes. I am doing this via: Dim asm As = .LoadFrom(MyDLLPath) Dim TypeToLoad As Type = asm.GetType(MyDLLName &...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
2
by: Kimmo Laine | last post by:
I tried to use a class member function as a callback in preg_replace_callback, but failed. It announces: " Warning: preg_replace_callback() : requires argument 2, 'myclass::myfunction', to be a...
2
by: KimRaver | last post by:
How do i declare a method that returns a doublepointer to a subclass, outside the Class-definition ? That is, as of now I have Class House { ... ... Class Room {
1
by: masterofthebus | last post by:
Hi everyone, I have a question that is tricking me. I need to do something like this: ----------------------------------------------------------- int a;
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.