|
|||||||
| PHP Discussion and technical support for using and deploying PHP based websites. |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Calling Functions from Other Functions
I'm sure its possible to call functions from other functions, but it is not working for me. Is there some syntax someone can provide to me as an example of calling a function within another function?
Also, both of these functions are in the same class. I have not declared any functions in this class as public or private. Do functions in a class default to private? Or do functions default to public? If they are public by default, are functions in the same class in each other's scope? Thanks! |
|
#2
|
||||
|
||||
|
Here is a prior discussion with a little relevance:
http://www.webdeveloper.com/forum/sh...lling+function I think public is default..., but i also think that public and private visibility declaration on functions is only available as of php 5, and a lot of people still use php 4...so you may not need to declare depending on your target and enough planning. There is a good resource on visibility here: http://www.webdeveloper.com/forum/sh...lling+function
__________________
We do precision guesswork Last edited by hastx; 03-16-2010 at 09:39 PM. |
|
#3
|
||||
|
||||
|
yes, private and public delarations of class variables and functions is available in php5
Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me ![]() To call a function with a class file in php5 use: Code:
$this->functionName(arg1,arg2,argN); Last edited by tirna; 03-16-2010 at 10:24 PM. |
|
#4
|
|||
|
|||
|
Thanks for the replies. I don't think I was clear in what I was asking. Basically, is this possible:
Code:
class someClass{
function_one($arg1)
{
$output_one = $arg1+15;
return $output_one;
}
function_two()
{
$send = 3;
$output_two = function_one($send);
return $output_two;
}
}
Thanks. |
|
#5
|
||||
|
||||
|
A function defined in a class does not "exist" only by its function name, you must reference it via the "->" operator as a member of an object, or statically via the "::" operator as a class member. Therefore in your example you would want to use the special "$this" variable to indicate that the function is a method of the current object:
PHP Code:
__________________
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ Terry Pratchett in Nation Kindle Minds (blog about Amazon Kindle) |
|
#6
|
||||
|
||||
|
Quote:
![]() Code:
yes, private and public delarations of class variables and functions is available in php5 Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me :) To call a function with a class file in php5 use: Code: $this->functionName(arg1,arg2,argN); |
|
#7
|
||||
|
||||
|
__________________
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ Terry Pratchett in Nation Kindle Minds (blog about Amazon Kindle) |
|
#8
|
|||
|
|||
I did misunderstand that post! However, the "$this->" solved my problems. Thanks so much for all who answered!
|
![]() |
| Bookmarks |
| Tags |
| class, functions, php |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|