Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:36:14+00:00 2026-05-31T20:36:14+00:00

I am writing a set of classes that handle simple file uploads with both

  • 0

I am writing a set of classes that handle simple file uploads with both ftp and ftps (not sftp) protocols.

Since the only difference between using ftp and ftps in PHP is the connect function, I thought that I could use a variable function in the form $connection_function($host);, where $connection_function has the either of the values ftp_connect() or ftp_ssl_connect().

I made an abstract class that decides which to use. Simplifying for clarity, I wrote:

abstract class uploader {

  protected $connection_function;

  public function __construct($protocol) {
    if ( $protocol == "ftp" ) {
      $this->connection_function  = "ftp_connect";
    } elseif ( $protocol == "sftp" ) {
      $this->connection_function = "ftp_ssl_connect";
    }
  }

  public function upload($host, $file, ... ) {
    $conn = $this->connection_function($host);
  }
}

class ftp_uploader extends uploader {
  parent::__construct("ftp");
}

class ftps_uploader extends uploader {
  parent::__construct("ftps");
}

I get the error

PHP Fatal error: Call to undefined method ftp_uploader::connection_function()

Which obviously comes from
public function upload($host, $file, … ) {
$conn = $this->connection_function($host);
}

So it occurs to me that I’ve written a variable method instead of a variable function, which is apparently not allowed (or treated as I expect it to be) in PHP.

Can I do what I want? The abstract class handles all of the ftp functions, and I would like to avoid duplicating code over two classes.

Edit Now that I’ve looked at it a bit, I see that I can just put the protocol decision in the conection method itself:

  public function upload($host, $file, ... ) {

    if ( $protocol == "ftp" ) {
      $conn = ftp_connect($host);
    } elseif ( $protocol == "sftp" ) {
      $cont = ftp_ssl_connect($host);
    }

  }

But still I wonder, is there a way to do my variable methods? As it is, I still have to duplicate my expection-throwing error handling and any other code I might want to put inside those if-blocks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:36:16+00:00Added an answer on May 31, 2026 at 8:36 pm

    As far as the interpreter knows, you’re trying to call that function, not use is as a variable:

    public function upload($host, $file, ... ) {
      $conn = $this->connection_function($host);
    }
    

    Instead, try this:

    public function upload($host, $file, ... ) {
      $func = $this->connection_function;
      $conn = $func($host);
    }
    

    And as crashspeeder mentions, you can also use call_user_func(), here’s a slightly modified version of his answer:

    public function upload($host, $file, ... ) {
      $conn = call_user_func_array($this->connection_function, func_get_args());
    }
    

    For this specific use case, that’s likely the best solution, as you can easily pass on the arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
I'm writing an winforms app that needs to set internet explorer's proxy settings and
When I'm writing my DAL or other code that returns a set of items,
I'm writing a bash script and I have errexit set, so that the script
I'm writing a plugin that will allow parameters to 'set it up.' But I
I'm writing a set of collection classes for different types of Trees. I'm doing
I have a set of classes that represent the storage of a font format
I am writing a set of database-driven applications in PHP. These applications will run
I'm writing a set of functions in c++ which can be called by excel.
I am writing a bash script to set up a production server. The tasks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.