Welcome Guest, Not a member yet? Register   Sign In
Correct way to use homemade functions
#9

[eluser]John_Betong[/eluser]
Hi,
 
The way I understand this is a learning experience for OOPS:
 
1. Test and include a helper file.

2. Set change the $data['message'] in the "my_helper.php" file

3. Output the results in a view file.
 
 
So...
 
The naming, linking the my_helper file is OK but the function hello() is oblivious of the $data array and coughs up the following: "Message: Undefined variable: message"
 
 

One way of solving this as suggested by pyromaniac is to make the variable global throughout your program:
function hello() {
$_GLOBAL['data']['message'] = 'Hi there from my_helper->hello()';
}
 
 

A second method is to make the whole $data array global:
function hello() {
global $data;
$data['message'] = 'Hi there from my_helper->hello($data) ';
}
 
 

A third method is to pass the $data variable as a VALUE parameter AND return the amended $data array:
function hello( $data ) {
$data['message'] = 'Hi there from my_helper->hello($data) ';
return $data;
}
 
But using the above method in your Controller you will have to change the following line:
$data = hello($data);
 
 
 


A fourth method is to pass the $data as a REFERENCE parameter (which does not require a return value).
function hello( $data ) {
$data['message'] = 'Hi there from my_helper->hello($data) ';
}
AND in your Controller to change the passed $data array by REFERENCE:
hello( $ $data );

 
That has woken me up, time for my second coffee and back to the grind stoneSad
 
Cheers,
 
John_Betong


Messages In This Thread
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:27 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:34 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:36 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:37 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:42 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:43 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:45 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 02:17 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 08:58 PM
Correct way to use homemade functions - by El Forum - 08-15-2007, 01:08 PM
Correct way to use homemade functions - by El Forum - 08-15-2007, 01:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB