开发者

In PHP how do you create reusable objects?, is there a best practice to this? What do you prefer?

开发者 https://www.devze.com 2023-04-01 16:37 出处:网络
I am creating some reusable objects in php, and I wanted to know whats the best way to build them. Below I have 2 examples of different ways of doing this.

I am creating some reusable objects in php, and I wanted to know whats the best way to build them. Below I have 2 examples of different ways of doing this.

Class Uploader{
    public $Filename;
    public $Directory;

    function upload(){
        upload_file($this->Filename, $this->Directory)
    }
}

// Then use the class above like this.
$u = new Uploader;
$u->Filename = 'foo.png'; // Set all the props
$u->Director开发者_如何学JAVAy = 'bar/'    //  ^   ^   ^    ^
$u->upload();             // Then Execute

Or would it be better to do this...

Class Uploader {
    function uploader($filename, $directory){
        upload_file($filename, $directory)
    }
}

// Then use the class above like this.
$u = new Uploader;
$u->uploader('foo.png', 'bar/') // Obviously much less code, All in One.

Out of these two methods, which one is preferred, is their a speed difference or any sort of gain of using one over the other?

I favour example #1, but is their a best practice to this?


Why can't you do both?

class Uploader
{
  public
    $filename,
    $directory;

  public function __construct( $name = '', $dir = '', $autoUpload = false )
  {
    $this->filename = $name;
    $this->directory = $dir;
    if ( $autoUpload )
    {
      $this->upload()
    }
  }

  public function upload()
  {
    //check your values
    ...code...
    upload_file( $this->filename, $this->directory );
  }
}

With this technique, you could automatically upload a file simply with:

$uploader = new Uploader( $name, $dir, true);

or you could manually construct the object with:

$uploader = new Uploader();
$uploader->filename = $name;
$uploader->directory = $dir;
$uploader->upload();


Method one is a classic OO approach where the object you create houses the data and the methods to act upon that data. Method two is simply creating a utility library of functions within a class. Method two is undoubtedly faster, but less OO in its approach. If you are shooting for reuse, I would actually go with method one. If it is performance you want, skip using classes altogether and write a functional library.


Daniel Pereira is right about performance.

To mix the two examples (perfomance & reuse), you can try:

Class Uploader{
    public $Filename;
    public $Directory;

    function Uploader($this->Filename, $this->Directory){
       upload_file($this->Filename, $this->Directory);
    }
}
$a = new Uploader('foo.png','bar');
echo $a->Filename; //foo.png
echo $a->Directory; //bar

It should be actually (because of a mistake):

Class Uploader{
        public $Filename;
        public $Directory;

        function Uploader($Filename, $Directory){
            $this->Filename = $Filename;
            $this->Directory = $Directory;
           upload_file($this->Filename, $this->Directory);
        }
    }


If you want to do true OO, the first example is pretty good. Another suggestion would be this:

Class Uploader{
    private $Filename;
    private $Directory;

    function upload(){
        upload_file($this->Filename, $this->Directory)
    }

}

Then you can create setFileName and setDirectory methods so you abstract out the setting of those fields for later.

You can also create a constructor with those fields in it. Many ways to solve this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号