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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:18:43+00:00 2026-06-12T18:18:43+00:00

I am new to OOP and I have recently asked question , which have

  • 0

I am new to OOP and I have recently asked question, which have thaught me that:

  • every “operation” should be implemented in seperate class,
  • issue I have faced should be resolved with dependency injection,
  • I must not have one object for entire application.

Let’s say I have the following code:

class FtpConnect {
     public $server;

     private function connect () { ... }

     public __construct ($db, $mailer, $server) {
         $this->server = $server;
         $this->connect();
     } 
}

class Database {
    public $database_property;
    ...
}

class Phpmailer {
    public $phpmailer_property;
    ...
}

$db = new Database();
$mail = new Phpmailer();

$ftp = new Ftpconnect ($db, $mail, "ftp.mozilla.org");

$ftp->db->database_property;
$ftp->mail->phpmailer_property;

Is this a proper approach? It seems that I still have one object $ftp.

Reading property or calling methods like $ftp->db->database_property; is this the proper way?

  • 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-06-12T18:18:44+00:00Added an answer on June 12, 2026 at 6:18 pm

    There are a few good things and a few bad things in your example! First, congratulations for your serious attempt to learn good programing practices. You’re going the right direction!

    Good things:

    • you’re using proper constructor injection (form of DI) and therefore you are only writing new statements on the highest level of your example and none inside of classes.
    • you have written a class for each of the different services, great.

    Bad things:

    • you’re accessing properties of db and mail through the ftp instance, don’t do that!
    • you’re using public properties, make properties private and use public getters and setters
    • you’re calling main methods in in the constructor, don’t do that, you should not call it inside the class but outside.
    • you’re not doing anything with the $mailer instance passed to the ftp instance, does the ftp class really need the mailer class at all?

    Improved example, assuming your ftp class really needs both database and mail:

    class FtpConnect {
    
         private $db;
         private $mailer;
    
         private $server;
    
         public function connect () { ... }
    
         public __construct ($db, $mailer, $server) {
             $this->setServer($server);
             $this->setMailer($mailer);
             $this->setDb($db);
         }
    
         public setServer($server)
         {
             $this->server = $server;
         }
    
         public setDb($db)
         {
             $this->db = $db;
         }
    
         public setMailer($mailer)
         {
             $this->mailer = $mailer;
         }
    }
    
    class Database {
        private $databaseProperty;
        public getDatabaseProperty()
        {
            return $this->databaseProperty;
        }
    }
    
    class Phpmailer {
        private $phpmailerProperty;
        public ... see above...
    }
    
    $db = new Database();
    $mail = new Phpmailer();
    
    $ftp = new FtpConnect ($db, $mail, "ftp.mozilla.org");
    $ftp->connect();
    
    $someProperty = $db->getDatabaseProperty();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to OOP and I have a question that's gotta be pretty
I am fairly new to PHP OOP, the problem that I am have is
New to OOP with Perl, and just had a quick question. I have this
I am new to OOP and have a class, inside this class I am
I'm pretty new to OOP, so any help is appreciated. I have a class
I am new to OOP in PHP (normally write software). I have read that
I'm quite new to PHP OOP and I have a question regarding a simple
I have a OOP question. I have 3 classes. Program (main class), login form
I'm new to OOP AS 3.0, so.. I have a question. I have two
I'm rather new to OOP and have a question about how to best deal

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.