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 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

Created a WCF service that is being called by a Silverlight app. Both reside
i created a simple search engine that displays mysql database results using the php
I created WCF service that returns a custom object called XmlElementTreeNode. This is what
Created an extensive batch script program to handle some automated file management and printing
created a new IIS7 web site with Intergrated .net 4.0 app pool. URLs ending
created a sqlce Db with VS2008 and save a copy in C:\ Temp08. can
I created two nib files for the iPhone and iPad, so my app will
I created Get/Set HttpContext Session Methods in BaseController class and also Mocked HttpContextBase and
I created my website with jQuery mobile and I would like to build the
I created a brand new project using the default Web Application template and I

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.