Forum Moderators: coopster
i always use functions accessible to all...
now i want to have a private and public funtions.. how do i do that...?
<?
function loadX()
{
function loadY()
{
//i want this function open to public
return 'PUBLIC';
}
function loadZ()
{
//i want this function in private access
return 'PRIVATE';
}
echo 'main function';
}
echo loadX(); // this will print the "main function"
echo loadY(); // this will print the "PUBLIC"
echo loadZ(); // must not print the "PRIVATE" but can access within the main function loadX()
please give me a good start about this matters... thanks advance...
?>
the only reasonable way to do that would be for me to create a file private.php with all private functions in it and in the header add a line:
include_once('public.php');
if (validate_user()) include_once('private.php');
I don't see any other, as simple solution.
function validate may be stored in public.php and is there to check if user has permission to access that the private part or not.
Hope this helps you!
Michal
PS. I'm not sure, but I don't think that you can create a function in a function, syntax:
function A () { function B() {}} seems wrong to me...
Give it a try.
<?php
//simulate encapsulation
//calling script
require_once(/publicfns.inc);
publicfn();
privatefn(); //Warning: call to member function on a non-object (or similar error)
?>
<?php
//publicfns.inc
function publicfn() {
//include then call the private function
require_once(/privatefns.inc);
//make instance of class PrivateFN()
$privatefns = new PrivateFNs();
//call privatefn()
$privatefns->privatefn();
//destroy instance of class PrivateFN()
$privatefns = null;
}
?>
<?php
//'private' class
class PrivateFNs {
function privatefn() {
//code...
}
}
?>
[edited by: darrenG at 11:13 am (utc) on June 22, 2007]