0

I want to be able to create methods within __construct

I tried using lambda in the following way

// This is what I am trying to achieve as an end result
class A extends Z{
    public function __construct() {
        parent::__construct()
    }

    public function myfunc() { // do something }
}


// This was an attempt to implement it
class A extends Z{
    public function __construct() {
        //example
        if ($something_is_true) {
            $this->myfunc = function () {}
        }
        parent::__construct()
    }
}

I hope this explains what I am trying to achieve

EDIT

I have URLs mapped to functions, and I want to have some logic to determine what URL-mapped-functions exist on the class

5
  • I have URLs mapped to functions, I don't want all subclasses of A to have every single mapped URL created Commented Jan 24, 2013 at 3:24
  • 2
    But if you want to define it in the constructor as a function to be over-ridden, what would be the difference between trying to define it there instead of normally? I think that I understand the question, just not the point. Commented Jan 24, 2013 at 3:28
  • It wouldn't be a function to be overriden, there would be logic to determine if it should be created in the first place.... Commented Jan 24, 2013 at 3:41
  • Oooh, ok. I apologize, that didn't seem to be apparent. The answer here so far seems like it would be a good solution for you then. However, because of how PHP works, it would all be loaded in to memory regardless. Commented Jan 24, 2013 at 3:49
  • Yeah, I should have made that clearer :) I come from a python background where you can use meta-classes. But that method should be ok despite being loaded in memory. thanks for the interest and your help! Commented Jan 24, 2013 at 4:25

2 Answers 2

2

Don't think that's possible. But another way to dynamically 'create' methods within a class is to use the magic __call() method.

With this method implemented, any attempt to call a non-existent method on an object will call the __call() method instead. The first parameter passed in will be the name of the function that the user called, and the second parameter will be the arguments.

See the manual for further details.

Sign up to request clarification or add additional context in comments.

Comments

0

Once a class has been defined, you can't add new methods to it using standard PHP. I'd advise using __call as well but you can also accomplish what you want, if you're ok with using variable variables, by creating the function contents dynamically with create_function:

<?php

class A
{
    public function __construct()
    {
        $this->addUpEchoAndReturn=create_function('$a,$b','$c=$a+$b;echo "$a+$b=$c<hr />";return $c;');
    }
    public function add_up_and_echo($a,$b,$c,$d)
    {
        $fn=$this->addUpEchoAndReturn;
        return $fn($fn($a, $b),$fn($c, $d));
    }
}


$x=new A();
$result=$x->add_up_and_echo(3,4,5,25);
echo "<hr /><hr />Final Result: $result";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.