3

Could someone help me understand how I can make functions within class methods, that would allow me to do things like this:

$class->send->activation_email()

I've seen many APIs do this, so I've tried:

class MyClass 
{

    public function send()
    {

        function activation_email()
        {
            echo "success!";
        }

    }
}


Undefined property: MyClass::$send 
2
  • 2
    In the example $class->send->activation_email() "send" is also a (instance of a) class not a function. If it was a function, it would be $class->send()->activation_email(), and it would have to be a function that returned an instance of a class that had a activation_email method in it. Commented May 16, 2014 at 19:32
  • Whoever just deleted their answer, i think it worked! Commented May 16, 2014 at 19:40

4 Answers 4

4

Consider:

class emailSender()
{
   function activation_email()
   {
      if (mail($this->to, $this->subj, $this->body)) {
          print $this->msg;
      }
}

class MyClass 
{
 var $send;
 function __construct()
 {
    $this->send=new emailSender();
    $this->send->msg="success!";
  }
}

$obj=new MyClass();
$obj->send->activation_email();
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, so really that first method is just a declaration of a separate class, and the function called is within that second class. ... interesting...
1

You probably want to an instance of another class within your class. Make a variable inside your class like this:

$this->otherclass = new Otherclass();

In this case, you can call functions from your other class the following way:

$myClass->otherclass->otherClassFunction()

Comments

0

Someone had answered but deleted the post. This is along the lines of what I was hoping for, and it works as expected:

class MyClass {

    public function send(){
        echo "Sending: ";
        return $this;
    }

    public function activation_email(){
        echo "activation email.";
    }
}

$myClass = new MyClass();
$myClass->send()->activation_email();

Comments

-2

What you are referring to (shame on no one for noticing this, for shame) is called "method chaining". A lot of big frameworks do this. Consider this example of use:

echo $obj->setName('Mike')->convertMtoN()->getName();
//  Echoes "Nike"

Cool.

But here is how it works:

class Example {

    private $name = '';

    public function setName($name) {
        $this->name = $name;

        //  We return the object, so you can call it again.
        return $this;
    }

    public function convertMtoN() {

        // Let's do Caps first
        $this->name = str_replace("M", "N", $this->name);

        // Then lowercase
        $this->name = str_replace("m", "n", $this->name);

        // We return the object, keep working
        return $this;
    }

    public function getName() {
        return $this->name;
    }
}

$name = new Example;

echo $name->setName('Mike')->convertMtoN()->getName();

Essentially, for each method that does not implicitly return a value, you simply return the object, allowing you to continue chaining.

Awesome, right?

PHP rocks (now, I know it has its faults, but with HHVM and process forking, it basically rocks [dude, you will get there]).

You can play with this here: https://ideone.com/fMcQ9u

13 Comments

Wait, what? What's not a method?
This site is note a personal help forum for OP. So while this might possibly answer OP all other future readers are utterly confused while your keyboard has apparently some sticky keys.
Popular request: curb your enthusiasm, focus on accuracy.
@MichaelJMulligan I think you should seriously consider modifying your tone. One could examine the credibility of the people you're interacting with here, and possibly summon up a little embarrassment. I'm embarrassed on your behalf. PeeHaa knows what HHVM is, and I assure you he understands it quite well. I would hazard that he understands it better than you. I would also spare the lectures and walk away. Your call.
I'd just like to add to what Chris said, and note that unlike PeeHaa, @ircmaxell is a complete PHP noob with no php experience whatsoever. Also, you should consider why you're getting that feedback.
|

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.