-1

Very simple setting, but I simply don't get it...

functions.php

class functions {
    function output_functions() {
        function sendMail($to, $subject, $message){
            //...code...
        }
    }
}

myfile.php

include $_SERVER['DOCUMENT_ROOT'] ."some/subfolders/functions.php";

$myFunction = new functions();
$myFunction->output_functions()->sendMail("mail@mail.com","My Subject","My Message");

And I'm getting the Error:

Call to a member function sendMail() on null

Any help will be highly appreciated!

user3532637
  • 333
  • 3
  • 16

3 Answers3

2

If you want to group your functions by type (output_functions, input_functions, whatever) I'd do a different class for each. You then should define them as public or private.

As it already has been said, nested functions cannot be accesed from outside, they're just available inside the scope of the function in which they are defined.

jonas3344
  • 201
  • 1
  • 7
0

Where did you find such a syntax?

class Helper_Output {

  static public function sendMail($to, $subject, $message){
            //...code...
        }
}

//and usage: 
Helper_Output::sendMail("mail@mail.com","My Subject","My Message");
bato3
  • 2,695
  • 1
  • 18
  • 26
0
class Methods1 {
  public function do1() {
    echo __FUNCTION__ . PHP_EOL;
  }

  public function do2() {
    echo __FUNCTION__ . PHP_EOL;
  }
}

class Methods2 {
  public function do1() {
    echo __LINE__ . PHP_EOL;
  }

  public function do2() {
    echo __LINE__ . PHP_EOL;
  }
}

class Factory {
  public function getMethods1() {
    static $cache = new Methods1;

    return $cache;

  }

  public function getMethods2() {
    static $cache = new Methods2;

    return $cache;

  }
}

Usage:

$factory = new Factory;
$factory->getMethods1()->do1();
$factory->getMethods1()->do2();
$factory->getMethods2()->do1();
$factory->getMethods2()->do2();
Niclas Larsson
  • 1,317
  • 8
  • 13
  • It's generally a good idea to include at least some explanation of how the solution works and how it solves OP's problem – William Perron Feb 21 '18 at 13:39
  • @WilliamPerron yes, but this code is so simple and self explaining. Two methods that returns two different classes with different methods. Just as the topic creator wanted to do. – Niclas Larsson Feb 21 '18 at 15:17