0

Is it possible to restrict a function written in a class constructor to certain methods within a class like so:

class Myclass {

public function __construct()
{
    // run a function here, but make it assessible to specific methods
    // method_one(), method_two(), method_three()
}

public function method_one()
{
 
}

public function method_two()
{

}

public function method_three()
{

}

public function method_four()
{
    // function should not be assessible here
}

}

I understand that the function can simply be put in the methods where it is needed, but what if I have so many methods like that, say 10, won't it mean that the function will be repeated so many times?

EDIT

Here's the live scenario:

I'm building a MVC app that checks if a user has updated his/her profile details. The controller class has methods that are called when a page is visited. So, I want for every page that is visited (apart from the profile page) to redirect to the profile page if the profile hasn't been completed.

So, with the current code structure I have, is this possible?

Samuel Asor
  • 480
  • 8
  • 25
  • This doesn't seem like something that would lead to clean scripting. Just call what you need inside those specific methods, right? How about: https://stackoverflow.com/q/26746988/2943403 – mickmackusa Dec 26 '20 at 10:51
  • Yeah, right. Just wanted to know if it is possible calling it in the constructor. – Samuel Asor Dec 26 '20 at 10:53

0 Answers0