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

  • Home
  • SEARCH
  • 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 3999188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:37:29+00:00 2026-05-20T07:37:29+00:00

I created an ssh2 wrapper. I have read that a constructor should not fail,

  • 0

I created an ssh2 wrapper. I have read that a constructor should not fail, so the ssh2 connection is not done in the wrapper, but by a method connect(). My question is: how do I make sure that connect() is called? I also really only need it to be called once.

class uploader {
   private $user; private $pass; private $host;
   private $ssh = null; private $sftp = null;
   public function __construct($host, $user, $pass) {
      $this->host = $host; $this->user = $user; $this->pass = $pass;
   }
   public function connect() {
      if ($this->ssh === null) {
         $this->ssh = ssh2_connect($this->host);
         ssh2_auth_password($this->ssh, $this->user, $this->pass);
         $this->sftp = ssh2_sftp($this->ssh);
      }
   }
}

What is the best way to ensure that connect() is called? Should the application call it?

$ssh = new uploader('host', 'user', 'pass');
$ssh->connect();

Or in the class methods?

...
public function upload($source, $dest, $filename) {
   $this->connect();
   ...
}

public function delete($file) {
   $this->connect();
   ...
}

Neither of these seems ideal.

I also thought about making a static method that would wrap the constructor and connect, but then the constructor would have to be private and I have also read that static methods are undesirable (mostly just for unit testing).

  • 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-20T07:37:30+00:00Added an answer on May 20, 2026 at 7:37 am

    I have also read that static methods are undesirable (mostly just for unit testing).

    Static methods are undesirable for some things, but factory methods isn’t one of them. They make perfect sense there and do not impact unit testing. So, go ahead and make a static factory method.

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

Sidebar

Related Questions

I created the setup project for the application and I can see that the
How to connect to a AWS instance through ssh? I have: Signed up at
Created a stored procedure in SQL 9 (2005) and have since upgraded to SQL
created an Eclipse plugin that contributes to the UI by a new project wizard
Diagrams created with Microsoft SQL Server Management Studio have their default schema set to
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I created a few mediawiki custom tags, using the guide found here http://www.mediawiki.org/wiki/Manual:Tag_extensions I
I created a single page (with code behind .vb) and created Public intFileID As
I created an Interop user control in VS2005. When the user control is shown
I created a simple .NET windows application in Visual Studio 2005 and on just

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.