I want to prevent the function being called from outside, so the flag dosen't really help so much.
Main Topics
Browse All TopicsI have this class:
class someclass {
function dosomething() {
echo "Where we called by causesomething() or from outside the class?";
}
function causesomething() {
$this->dosomething();
}
}
I want to diferentiate inside the dosomething() function between:
$class->dosomething();
and
$class->causesomething();
ie I want to know inside the dosomething() function if we where called from within the class.
Needs to work on PHP4, can't just use a private status unfortunatly.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Depending on the actual scenario you may be able to shuffle the logic, eg:
<?php
class someclass {
function dosomething($flag=0) {
if ($flag == 1) {
echo 'called from within class';
}
}
function causesomething() {
$this->dosomething(1);
}
}
$class = new someclass();
echo 'calling via: $class->dosomething(): ';
$class->dosomething();
echo '<br>calling via: $class->causesomething(): ';
$class->causesomething();
?>
I will stand back and see if anyone else has an idea now if a flag isn't going to work here.
Diablo84
I'm pretty sure a flag is the optimal solution in php4 as Diablo stated.
You could set and unset a GLOBAL variable or class variable inside the desired public method so you don't have to worry about parameters, but either way you're going to have to do a little bit of extra work to get around the OO limitations of php4.
I tend not worry about this and just place the desired usage(public, protected, or private) within comments above each class method, and then add them in when you are able to use php5.
I am actually trying to make a license.php file here and want a private function that can't be accessed by the "outside" world, ie it will decode some encrypted data and I don't want that data directly accessable. I would prefere not to have to reuse the decode process in each function that needs to decode it.
I guess I thought there would be a function like is_inclass(). I know there is a way for example of telling if a function is called statically, I am surprised you can't tell if you where called from inside/outside the class.
Here is a solution that might do what you're looking for. It's probably not optimal but it gives an example of how to store private data that can only be accessed from certain methods.
I must reiterate the PHP really isn't a good choice of language if you need data security of this type. Consider a language with more evolved private/protected object orientation.
Here is my idea:
<?php
// Usage
$L = new LicenseObject();
$L->checkLicense('AAABBBCC
if($L->checkLicense()){
print("Your license is OK\n");
// do protected stuff
}
class LicenseObject{
function LicenseObject(){
}
function setOrRetrievePrivateData($
static $storedkey;
static $storeddata;
if(is_null($data)){
if($key==$storedkey){
return $storeddata;
}
die("Invalid access to stored data");
}else{
if($data!="AAABBBCCC"){
die("Invalid license");
}
$storeddata=$data;
$storedkey=$key;
return true;
}
}
function checkLicense($data=NULL){
static $storedkey;
if($data){
// License data has been submitted, check it and store in private variable
$storedkey=md5($data);
if($this->setOrRetrievePri
return true;
}else{
return false;
}
}else{
// License data has not been submitted, just check it
if($this->setOrRetrievePri
return true;
}else{
return false;
}
}
}
}
?>
if you can use php >= 4.3
class someclass{
function iAmPrivate(){
$callers=debug_backtrace()
if (isset($callers[1]) && isset($callers[1]['class']
// everything is good, i am called by a class method
return true;
} else {
trigger_error('Unauthorize
}
}
function iAmPublic(){
$this->iAmPrivate();
}
}
$c=new someclass();
$c->iAmPublic(); // returns true
$c->iAmPrivate(); // error
someclass::iAmPrivate; // error
JP, the only way i can think of is very un-OOP (no surprise with PHP4 ;) )
class someclass{
function getX(){
return $this->x();
}
function setX($y){
$this->x($y);
}
function x($y=null){
static $x;
$callers=debug_backtrace()
if (isset($callers[1]) && isset($callers[1]['class']
if ($y!==null) {
$x=$y;
}
} else {
trigger_error('Please use setX() and getX()', E_USER_ERROR);
}
echo "$x\n";
return $x;
}
}
now you have 'hidden' $x (like a private variable) and can access it only with getter and setter classes. Use _only_ if you need php4 :(
Business Accounts
Answer for Membership
by: Diablo84Posted on 2005-05-10 at 18:49:00ID: 13974493
There might be a better approach but the most efficient method that comes to mind is to use a flag, for example:
<?php
class someclass {
function dosomething($flag=0) {
if ($flag == 1) {
echo 'called by causesomething()<br>';
}
else {
echo 'called outside of the class<br>';
}
}
function causesomething() {
$this->dosomething(1);
}
}
$class = new someclass();
$class->dosomething();
$class->causesomething();
?>
Diablo84