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

  • Home
  • SEARCH
  • 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 6391121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:38:00+00:00 2026-05-25T03:38:00+00:00

I am creating some reusable objects in php, and I wanted to know whats

  • 0

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->Directory = '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?

  • 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-25T03:38:01+00:00Added an answer on May 25, 2026 at 3:38 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am dynamically creating some classes and I want them to have different docstrings.
I am creating some build scripts that interact with Perforce and I would like
I am creating some multi-threaded code, and I have created a JobDispatcher class that
I am creating a C# library with some reusable code and was trying to
I have been tasked with creating a reusable process for our Finance Dept to
I am creating some always-on-top toasts as forms and when I open them I'd
I'm creating some web services using JAX-WS and the java SE build-in server. Every
I'm creating some re-usable functions, and right now I have error handling setup like
I am creating some custom selectboxes with jQuery so I can style them exactly
I am creating reusable Data Access Layer for SQL Server for some projects. 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.