0

Say I have a function from an Object :

class Cat {
  protected $sound = 'MeOwWw~';

  public function make_a_big_and_nice_sound () { echo $this->sound; }
}
$C = new Cat;
$C->make_a_big_and_nice_sound ();

Now, the function's name could be long and the content depends on some properties of the object itself so It can't be rewrite outside of the Object.

But let's say I kind of have only one cat to birth and I want to make it meow a lot of time in my code here and there. What I want to do is the following :

main.php

function please_meow = $C->make_a_big_and_nice_sound;
please_meow ();
vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • 1
    This question might be related: http://stackoverflow.com/questions/1688711/can-we-alias-a-function-in-php – MDEV Jan 09 '16 at 20:38

3 Answers3

2

If your method names are long there is a big chance your method is simple doing too much. A method should only do one single thing.

So you should really actually fix your code instead.

Considering you don't provide your actual code (which would have helped pointing out the actual flaw in your code) and if you really insist on doing this (you really shouldn't) you could use a closure for this:

$please_meow = function() use ($C) {
    return $C->make_a_big_and_nice_sound();
};

$please_meow();

But again if you need this you are doing it wrong.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jeeves
  • 208
  • 2
  • 8
1

If you use PHP 5.3 or above, try this:

$please_meow = function() use($C) { $C->make_a_big_and_nice_sound(); };
$please_meow();
clean_coding
  • 1,156
  • 1
  • 9
  • 14
-1

Now, the function's name could be long and the content depends on some properties of the object itself so It can't be rewrite outside of the Object.

But let's say I kind of have only one cat to birth and I want to make it meow a lot of time in my code here and there

You can do something like this

class Cat 
{
    protected static $instance;

    function __construct() 
    { 
        self::$instance = $this;
    }

    public static function please_meow()
    {
        self::$instance->make_a_big_and_nice_sound();
    }

    protected $sound = 'MeOwWw~';

    public function make_a_big_and_nice_sound () { echo $this->sound; }
}

new Cat();

Cat::please_meow();  
Cat::please_meow();

// some other codes

Cat::please_meow();
aayush93
  • 99
  • 1
  • 3
  • 1
    Read it. Still disagree with this answer. Creating global and most likely tightly coupled code is never (at least should never be) the answer. – Jeeves Jan 09 '16 at 21:34
  • Without seeing the actual code, you can't say this is not the right way to do it. You suggested the use of anonymous function, do you know PHP automatically converts anonymous function into instances of the closure internal class where as my code does it externally. – aayush93 Jan 09 '16 at 21:46
  • The difference between my code and yours is that your methods are `global`ly available and mine is scoped. Even worse it's global and error prone because `$instance` can be in an invalid state when calling it. – Jeeves Jan 09 '16 at 21:47
  • Read the op question again. The function depends upon the content of `object` not `class`. So i guess he will be creating the object before calling the static method which uses instance. Anyway you don't have to agree with me. – aayush93 Jan 09 '16 at 21:58