Steeven
Steeven

Reputation: 4210

Call a function in a class with an argument

I have problems sending an argument with a function / method call, when that function is inside a class.

I do this:

echo class_name::function_name('string_arg');

which succeeds in reaching the function function_name inside the class_name class. But the argument string_arg, which is a string, is not fetched when the function runs. It is like that function doesn't receive the argument or just doesn't read it.

For information: This call is made from inside another function within the same class.

Am I doing something wrong here with this :: class call method? It is confusing that I can reach the function, but nomatter the argument I still only get the functions default behavior.

Upvotes: 0

Views: 142

Answers (2)

Patrick Thach
Patrick Thach

Reputation: 128

I rarely see people using class::function_name() for non static method. Is there a reason why you use it?

Have you try this?

<?php echo $this->function_name($str); ?>

Upvotes: 1

stmfunk
stmfunk

Reputation: 683

This would depend on whether the function uses side effects. If this function simply returns a value then there will be no problem, as it doesn't need to interact with the class itself. I think the problem may be that you are attempting to call it as a class method outside the object your working in. When you add the class part to the front of the call i.e. this bit class_name::, your not actually calling the same class as the one in your current object, so it won't affect or use the fields in that object. If you just call function_name('string') then it will use the fields of the object your calling it from.

Upvotes: 1

Related Questions