Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:19:30+00:00 2026-06-14T19:19:30+00:00

Say we have a class with several protected and/or public methods. I need to

  • 0

Say we have a class with several protected and/or public methods.
I need to perform a check each time a method is called. I could do that check each time i call a method :

class Object
{
    // Methods
}

$o = new Object();

if($mayAccess) $o->someMethod();

or

if($mayAccess) $this->someMethod();

But i would like developers neither to have to think about it nor to write it. I’ve thought about using __call to do :

class Object
{
    public function __call($methodName, $args)
    {
        if($mayAccess) call_user_func_array($this->$methodName, $args);
    }
}

Unfortunatly, if i call the method from inside the class, __call will not invoked as it only works when a non-visible method is called.

Is there a clean way to hide this check for both internal and external calls ? Again the goal is to make sure a developper won’t forget to do it when calling a method.

Thanks in advance 🙂

EDIT :

I have another way of doing this :

class Object
{
    public function __call($methodName, $args)
    {
        if($mayAccess) call_user_func_array($methodName, $args);
    }
}

function someMethod() { }

But i won’t be able to use $this anymore, which means no protected methods, which i do need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:19:31+00:00Added an answer on June 14, 2026 at 7:19 pm

    No, I dont think so. What you could do though is write a proxy:

    class MayAccessProxy {
    
        private $_obj;
    
        public function __construct($obj) {
            $this->_obj = $obj;
        }
    
        public function __call($methodName, $args) {
            if($mayAccess) call_user_func_array(array($this->_obj, $methodName), $args);
        }
    }
    

    This means you have to instantiate a proxy for every object you want to check:

    $obj = new MayAccessProxy(new Object());
    $obj->someMethod();
    

    Ofcourse you’d also want the proxy to behave exactly like the object itself. So you also have to define the other magic methods.

    To make it a bit easier for the developers you could do something like this:

    class Object {
    
        /**
         * Not directly instanciable.
         */
        private __construct() {}  
    
        /**
         * @return self
         */
        public static function createInstance() {
            $obj = new MayAccessProxy(new self());
            return $obj;
        }
    }
    
    $obj = Object::createInstance();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a test class called testFixtureA with several methods testA ,
Say I have several objects within a class, each of which needs constructing with
Say I have several ObservableCollections of different classes: public class Employee { public int
say I have: class Test { public static int Hello = 5; } This
let's say I have class Dummy { String a, b; public Dummy(String a, String
Okay so let's say I have class A... CLASS A has a method that
Say I have a class with three constructors, like the one below: public class
Let's say I have a class designed to be instantiated. I have several private
Say you have a class Person (with name, age, etc), and an array called
I have a class, say the class Person. In than class I have several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.