Jump to content

Recommended Posts

i have a constructor

public function __construct(){

            // check if any action related to this page was generated

            if(isset($_GET['action'])){

                $this->action = ucwords($_GET['action']) . '()';

                self::{$this->maction};

                               

            }

           

            //code to generate the list of all the systems users

            $user = new userDAO;

            $this->results = $user->fetchAll();

      } 

 

 

 

if action is present in the querystring, eg. delete, it would be stored into $this->action as Delete().  Similarly, if the action was edit, it would be stored as Edit(). My intent is to call the method named Delete() or Edit() depending upon which action was generated. I have defined the methods in the same class. Once i assign the current action to the $this->action, i want to call the method without explicitly specifying the method name....

 

here i have tried self::$this->action....

i even tried $this->action only

 

but its not working? i think the string stored in the self::$this->action is not being interpolated? or....sth. i dont know. pls help out guys

     

Link to comment
https://forums.phpfreaks.com/topic/215690-method-calling-using-self/
Share on other sites

  self::{$this->maction};

 

I hope as that was a typo! I think that when you use self::action you do this instead of $this->action, but I'm not sure, I would have to read up on that to see, either will result in the same action so your syntax is more than likely wrong, probably should be:-

 

self::action;

 

Though I am probably wrong there, I seldom use self:: but I do use the 'scope resolution operator' :: outside the class to call functions statically..

 

Rw

well...lets not store the action in the object property...

 

i just want the action received from the query string to invoke the related method in the class....

if i got edit (or delete) action, i want it to be formatted into Edit() or Delete(), which i have done using

ucwords($_GET['action']) . '()';

 

How can i invoke the relevant method?

 

Currently, i've found out a solution just to please my boss...

 

 

public function __construct(){

    //check if any action related to this page was generated

    if(isset($_GET['action']{

             

      if($_GET['action'] == 'delete'){

          self::Delete();

      }

      if($_GET['action'] == 'edit'){

          self::Edit();

      }

    }

}

 

but i dont want to use if clause, i want to optimize it in such a way that when an action is received, we capitalize

the first letter, concat () and invoke the relevant method....

 

class SomeClass {    function __call($method, $args) {        if(method_exists($this, $method))            return $this->$method();        return NULL;    }        function call($method, $args) {        return $this->__call($method, $args);    }}

 

 

 

$class = new SomeClass();$output = $class->call($_GET['action']);

 

but i dont want to use if clause, i want to optimize it in such a way that when an action is received, we capitalize

the first letter, concat () and invoke the relevant method....

 

Look at my example again.

 

Don't bother adding the () as part of the string/variable, there not.

hi thorpe!!

thanks very much for your ans..it did indeed work for me.

 

in the mean time, your solution raised a new curiousity, will i be able to access this constructor's local variable

$action from another method in the class, like:

function Method(){
    echo $action;
}

$obj = new Class();
$obj->Method(); // so that $action is printed

 

i tried it and got a php notice:

Notice: Undefined variable: b in C:\wamp\www\test\index.php on line 31

my idea is that since $action variable is part of the constructor, it must be included in the $obj  unlike other private public and protected properties when we  instantiate an object for the Class(). isn't it so?

 

 

 

 

$action doesn't exist within other methods of your class. You would need to define it local to the object itself.

 

class Foo
{
  private $action;
  public function __construct() {
    $this->action = 'hello';
  }
  public function test() {
    echo $this->action;
  }
}

$obj = new Foo();
$obj->test();

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.