Skip to main content
added 225 characters in body
Source Link
myol
  • 235
  • 4
  • 11

MyThanks to thepacker, my revised thought is to separate the functionality into anone interface which gets a file from somewhere and ananother interface which does something to the file;

interface GetFromGetFile()
interface ConvertToConvertFile()

Thanks to thepacker for leading me to this train of thought;Then I could do

class GetFileFromUrl implements GetFile {...}

class ConvertFileFromZip implements ConvertFile {
    // coding to an interface
    public function __construct(GetFile $from) {...}
}

$zip_from_url = new GetFileFromUrl($url, $destination);
$file_path    = new ConvertFileFromZip($zip_from_url);

I just want to confirm this is the correct course of action in this case.

My revised thought is to separate the functionality into an interface which gets a file from somewhere and an interface which does something to the file;

interface GetFrom()
interface ConvertTo()

Thanks to thepacker for leading me to this train of thought; I just want to confirm this is the correct course of action.

Thanks to thepacker, my revised thought is to separate the functionality into one interface which gets a file from somewhere and another interface which does something to the file;

interface GetFile()
interface ConvertFile()

Then I could do

class GetFileFromUrl implements GetFile {...}

class ConvertFileFromZip implements ConvertFile {
    // coding to an interface
    public function __construct(GetFile $from) {...}
}

$zip_from_url = new GetFileFromUrl($url, $destination);
$file_path    = new ConvertFileFromZip($zip_from_url);

I just want to confirm this is the correct course of action in this case.

added 225 characters in body
Source Link
myol
  • 235
  • 4
  • 11

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromUrl then GetFileFromZip to get a zip file from a URL and unzip it?

So using just one would be

$file_from_url = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_url);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

My revised thought is to separate the functionality into an interface which gets a file from somewhere and an interface which does something to the file;

interface GetFrom()
interface ConvertTo()

Thanks to thepacker for leading me to this train of thought; I just want to confirm this is the correct course of action.

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromUrl then GetFileFromZip to get a zip file from a URL and unzip it?

So using just one would be

$file_from_url = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_url);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromUrl then GetFileFromZip to get a zip file from a URL and unzip it?

So using just one would be

$file_from_url = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_url);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

My revised thought is to separate the functionality into an interface which gets a file from somewhere and an interface which does something to the file;

interface GetFrom()
interface ConvertTo()

Thanks to thepacker for leading me to this train of thought; I just want to confirm this is the correct course of action.

added 908 characters in body
Source Link
myol
  • 235
  • 4
  • 11

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromDirectoryGetFileFromUrl then GetFileFromTarGetFileFromZip to get the file within a Tar archivezip file from a list of files within a certain directoryURL and unzip it?

So using just one would be

$file_from_dir$file_from_url = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_dir$file_from_url);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromDirectory then GetFileFromTar to get the file within a Tar archive from a list of files within a certain directory?

So using just one would be

$file_from_dir = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_dir);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

I am learning about coding to an interface in OOP programming. I understand you should code to an interface so you can 'swap out' functionality.

However, I have a situation where I feel I can reuse an interface again rather than simply swap it out.

interface GetFile
{
    public function getFile();
}

class GetFileFromUrl implements GetFile
{
    public function __construct($url, $destination_path) {...}

    public function getFile()
    {
        // download file from url and return path of file
        
        return 'file_path';
    }
}

class GetFileFromZip implements GetFile
{
    public function __construct($path, $extraction_path) {...}

    public function getFile()
    {
        // extract file from zip and return path of file
        
        return 'file_path';
    }
}

class DoStuff
{
    // coding to an interface
    public function doStuff(File $file) {
        $do_stuff = $file->getFile();
        //do stuff with file
    }
}

I thought this would allow the greatest amount of flexibility in code reuse in future as I can extend the interface further

class GetFileFromDirectory implements GetFile(...)
class GetFileFromTar implements GetFile(...)

I feel this a fairly good approach as I can pass whatever implementation of GetFile to DoStuff and it will execute readFile within the doStuff() method.

My question I am trying to ask, is how can I 'chain' these classes which implement GetFile together?

i.e. GetFileFromUrl then GetFileFromZip to get a zip file from a URL and unzip it?

So using just one would be

$file_from_url = new GetFileFromUrl($url_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($file_from_url);

But 'chaining' two I am currently doing;

$zip_file_path = (new GetFileFromUrl($url_path, $destination_path))->getFile();
$file_from_zip = new GetFileFromZip($zip_file_path, $destination_path);
$do_stuff      = (new DoStuff)->doStuff($zip_from_tar);

Is there a better / cleaner way of doing this?

added 908 characters in body
Source Link
myol
  • 235
  • 4
  • 11
Loading
Source Link
myol
  • 235
  • 4
  • 11
Loading