0

Is there a way to implement method pointers in PHP?

I keep getting the following error:

Fatal error: Call to undefined function create_jpeg() in /Users/sky/Documents/images.php on line 175

This is line 175:

if ($this->ImageType_f[$pImageType]($pPath) != 0)
class   CImage extends CImageProperties
{
  private $Image;
  private $ImagePath;
  private $ImageType;
  private function create_jpeg($pFilename)
  {
    if (($this->Image = imagecreatefromjepeg($pFilename)) == false)
      {
        echo "TEST CREATION JPEG\n";
        echo "Error: ".$pFilename.". Creation from (JPEG) failed\n";
        return (-1);
      }
    return (0);
  }
  private function create_gif($pFilename)
  {
    if (($this->Image = imagecreatefromgif($pFilename)) == false)
      {
        echo "Error: ".$pFilename.". Creation from (GIF) failed\n";
        return (-1);
      }
    return (0);
  }
  private function create_png($pFilename)
  {
    if (($this->Image = imagecreatefrompng($pFilename)) == false)
      {
        echo "Error: ".$pFilename.". Creation from (PNG) failed\n";
        return (-1);
      }
    return (0);
  }
  function __construct($pPath = NULL)
  {
    echo "Went through here\n";
    $this->Image = NULL;
    $this->ImagePath = $pPath;
    $this->ImageType_f['JPEG'] = 'create_jpeg';
    $this->ImageType_f['GIF'] = 'create_gif';
    $this->ImageType_f['PNG'] = 'create_png';
  }

  function __destruct()
  {
    if ($this->Image != NULL)
      {
        if (imagedestroy($this->Image) != true)
          echo "Failed to destroy image...";
      }
  }

  public function InitImage($pPath = NULL, $pImageType = NULL)
  {
    echo "pPath: ".$pPath."\n";
    echo "pImgType: ".$pImageType."\n";
    if (isset($pImageType) != false)
      {
        if ($this->ImageType_f[$pImageType]($pPath) != 0)
          return (-1);
        return (0);
      }
    echo "Could not create image\n";
    return (0);
  }
}
0

3 Answers 3

1

Just call the method you need with $this->$method_name() where $method_name is a variable containing the method you need.

Also it is possible using call_user_func or call_user_func_array

What is callable is described here: http://php.net/manual/ru/language.types.callable.php

So assuming $this->ImageType_f['jpeg']' must be callable: array($this, 'create_jpeg').

Alltogether: call_user_func($this->ImageType_f[$pImageType], $pPath) is the way to do it.

Or if $this->ImageType_f['jpeg'] = 'create_jpeg':

$this->{$this->ImageType_f['jpeg']]($pPath);

Some documentation on functions I mentioned here:

https://www.php.net/call_user_func https://www.php.net/call_user_func_array

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

1 Comment

You could also save the function name in a variable then call it: $funcName = $this->ImageType_f[$pImageType]; $this->$funcName($pPath);
1

Your problem is is line:

if ($this->ImageType_f[$pImageType]($pPath) != 0)

-since $this->ImageType_f[$pImageType] will result in some string value, your call will be equal to call of global function, which does not exists. You should do:

if ($this->{$this->ImageType_f[$pImageType]}($pPath) != 0)

-but that looks tricky, so may be another good idea is to use call_user_func_array():

if (call_user_func_array([$this, $this->ImageType_f[$pImageType]], [$pPath]) != 0)

Comments

0

I think you need to use this function call_user_func In your case call will looks like

call_user_func(array((get_class($this), ImageType_f[$pImageType]), array($pPath));

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.