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

I am creating some reusable objects in php and I wanted to know what is their best way to create them. Below I have two examples of different ways to do 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->Directory = 'bar/' // ^ ^ ^ ^ $u->upload(); // Then Execute 

Or it would 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. 

Of these two methods, which one is preferable, is it their speed difference or any gain from using one over the other?
I prefer example # 1, but is this the best practice for this?

+7
source share
4 answers

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 can automatically download a file, simply:

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

or you can manually create an object with:

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

The first method is the classic OO approach, in which the object you create contains data and methods for working with this data. The second method is simply to create a library of function functions within the class. The second method is undoubtedly faster, but less OO in its approach. If you shoot for reuse, I would go with method one. If this is the performance you want, skip the classes as a whole and write a functional library.

+6
source

Daniel Pereira is right about performance.

To mix two examples (performance and 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 

This should actually be (due to an error):

 Class Uploader{ public $Filename; public $Directory; function Uploader($Filename, $Directory){ $this->Filename = $Filename; $this->Directory = $Directory; upload_file($this->Filename, $this->Directory); } } 
+4
source

If you want to make a true OO, the first example is pretty good. Another suggestion would be as follows:

 Class Uploader{ private $Filename; private $Directory; function upload(){ upload_file($this->Filename, $this->Directory) } } 

Then you can create the setFileName and setDirectory methods, to then ignore the settings for these fields.

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

+2
source

All Articles