Forum Moderators: coopster

Message Too Old, No Replies

functions accessibility

any private and public functions?

         

camilord

5:35 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



need help about this...

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...
?>

mcibor

8:51 pm on Jun 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Cami,

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.

eelixduppy

9:19 pm on Jun 21, 2007 (gmt 0)



Can you define 'public' and 'private' for what you need them to be? Generally these terms refer to members in a class. Refer to the visibility documentation for more information: [php.net...]

darrenG

11:10 am on Jun 22, 2007 (gmt 0)

10+ Year Member



If you do not have a version that supports scope modifiers (public private...), you could try something like

<?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]

camilord

6:36 am on Jun 23, 2007 (gmt 0)

10+ Year Member



thank you thank you.. you help a lots... :bow: