1

Im trying to figure out how to call functions based on what a user clicks on a form. But im not sure if im doing it right.

I have a number of classes, lets say 3 for different ways to connect to a site, the user clicks on which one they would like.

  1. FTP
  2. SFTP
  3. SSH

Which i have named 'service' in my code.

I don't want to run a whole bunch of IF statements, i would rather try and build the call dynamically.

What i have at the moment is as follows

$ftp_backup = new FTPBackup;
$sftp_backup = new SFTPBackup;
$ssh_backup = new SSHBackup;

$service = $request->input('service') . '_backup';
$service->testConn($request);

Im getting the following error

Call to a member function testConn() on string

Im not sure im doing this right.

Any help would be greatly appreciated.

Thanks

3 Answers 3

5

First of all $service is a string on which You cannot call method, because it is not an object (class instance).

I think it is a great example of where You can use Strategy Pattern which look like that:

class BackupStrategy {
    private $strategy = null; 

    public function __construct($service_name) 
    {
        switch ($service_name) {
            case "ftp": 
                $this->strategy = new FTPBackup();
            break;
            case "sftp": 
                $this->strategy = new SFTPBackup();
            break;
            case "ssh": 
                $this->strategy = new SSHBackup();
            break;
        }
    }

    public function testConn()
    {
      return $this->strategy->testConn();
    }
}

And then in place where You want to call it You call it by:

$service = new BackupStrategy($request->input('service'));
$service->testConn($request);

I suggest You to read about Design Patterns in OOP - it will help You a lot in the future.

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

5 Comments

This is perfect .. It seems to simplify things beyond belief for me ... I have been studying SOLID principles a bit ... Thank you
You're welcome :) If You find it useful You may accept is as an answer.
Just one more thing if you dont mind ... Every *Backup() class implements an interface ... would this BackupStrategy just be a standalone class, or would i slot it in somewhere
Yes, it can be standalone because interface implementing is job dedicated and known only to each *Backup class. BackupStrategy class is only responsible for choosing proper *Backup class.
This has opened up my eyes to a whole other way of coding ... I really really appreciate this!
0

How about this:

$ftp_backup = new FTPBackup;
$sftp_backup = new SFTPBackup;
$ssh_backup = new SSHBackup;

$service = $request->input('service') . '_backup';
${$service}->testConn($request);

This is called "Variables variable": http://php.net/manual/en/language.variables.variable.php

Comments

0
// Create class name
$className = $request->get('service') . '_backup';

// Create class instance
$service = new $className();

// Use it as you want
$service->testConn($request);

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.