I have written an FTP class that is able to establish a connection.
class FtpConnection{
public $host = '';
public $user = '';
public $password = '';
public function ftpconn(){
$ftpConn = ftp_connect($this->host);
$login = ftp_login($ftpConn,$this->user,$this->password);
// check connection
if ((!$ftpConn) || (!$login)) {
echo 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
}else{
echo 'FTP connection was a success.';
//Place to use FTP PUT in PHP
}
ftp_close($ftpConn);
}
}
$newconn = new FtpConnection;
$newconn->host = '198.57.247.176';
$newconn->user = 'XXXXXXX';
$newconn->password = 'XXXXXX';
echo $newconn->ftpconn();
Question →
Can we improve this class definition making it more OOP in terms of relevance and code quality?
How can we work around making it more secure and using properties more correctly?
For a start, the echos in there don’t make any sense.
You echo the ftpconn method, but the method does not return anything, so it won’t echo anything. For that to work, it would have to return a string, which again, does not make much sense to me.
Then you have more echo within the method. Is it really the job of the method to echo things to the page?
Properties like $password could be made private properties, rather than public ones, then they can’t be accessed outside of the scope of the class. You would then need public methods to set them within the class.
But really it depends upon context. What is this for? How will it be used? What does it need to do?
$msg = 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
if ( $ftpConn && $login)
{
$msg = 'FTP connection was a success.';
}
echo $msg;
Since you have pointed that out I guess no I will rewrite some parts of the code and paste ity here for further discussion.
Nothing to be launched as a real live project sir. I just wanted to get some understanding in PHP FTP with OOPS. Trying to build a class catalog for FTP classes that can do many things.
public function ftpconn(){
$ftpConn = ftp_connect($this->host);
$login = ftp_login($ftpConn,$this->user,$this->password);
// check connection
$msg = 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
if ( $ftpConn && $login){
$msg = 'FTP connection was a success.';
}
echo $msg;
ftp_close($ftpConn);
}
class FtpConnection{
public $host = '';
public $user = '';
private $password = '';
public function __construct($host,$user,$password){
$this->host = $host;
$this->user = $user;
$this->password = $password;
}
public function ftpconn(){
$ftpConn = ftp_connect($this->host);
$login = ftp_login($ftpConn,$this->user,$this->password);
// check connection
$msg = 'FTP connection has failed! Attempted to connect to '. $this->host. ' for user '.$this->user.'.';
if ( $ftpConn && $login){
$msg = 'FTP connection was a success.';
}
echo $msg;
ftp_close($ftpConn);
}
}
$newconn = new FtpConnection('198.57.247.176','toolculator@app.trafficopedia.com','##');
echo $newconn->ftpconn();
This example is better but there is still room for improvement.
The properties can all be private. You do not need to assign an empty string to them.
You have mixed two different functions in your logic. How can you know what failed? Was it the connection that failed? Was it the login that failed? You can’t know the way it is. I will leave it up to you to see what you come up with.
The Success/Fail messages really dont belong in the Class/method. You should just be returning the ftp function return data. Decide how to handle the returns in your page code.
If you test them separate, you can know which failed.
Note I made $ftpConn and $msg into properties of the object.
This means the connection can be reused in the class, outside of the connection method once a connection is made.
If $msg is a public property, it can be accessed outside the class to report on status, eg.
$newconn = new FtpConnection('198.57.247.176','toolculator@app.trafficopedia.com','##');
if(!$newconn->ftpconn()){
echo $newconn->msg;
}
As you had it, $ftpConn was a variable, which is available only within the scope of that method. When the method has run, the connection is gone.
But the chances are, you would need to use the connection for something (or why connect?).
So I made the connection a property of the class/object, now it will be available for use outside of the scope on that method.