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 5982033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:01:44+00:00 2026-05-22T22:01:44+00:00

so i have a class which contains basic stuff like Class Test{ public function

  • 0

so i have a class which contains basic stuff like

Class Test{
   public function wheels(){
        echo 'wheels';
   }
}

and another one

Class Test2{
   public function bikes(){
       $test = new Test();
       return 'i am a bike with '.$test;
   }
}

so my question is if i have different classes that i need inside a function is it good practise to call it like $var = new ClassName();

or there is a better way to call different classes inside classes functions?

  • 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-22T22:01:45+00:00Added an answer on May 22, 2026 at 10:01 pm

    For your webapp write an autoloader that is just loading all classes from files you want to use while you need it.

    You can then just write the code like you want to w/o the need to build complex inheritance between classes only because you want to make use of the functionality you’ve coded.

    This has multiple benefits:

    1. Even w/o much experience in OOAD you keep stuff apart from each other by not introducing much relationship between multiple classes. Relationship between classes can be bad because you make things dependable on each other when it’s not really needed.
    2. You can easily extend and change your application. That’s important as you continue to code, be it now or later.

    That’s just my 2 cents. If you don’t like autoloader later on, you can change that, too. But it helps to get things on the run straight-forward.

    Example:

    function my_autoloader_function($name) {
        require(sprintf('%s/%s.php', $yourlibfolderdir, $name));
    }
    spl_autoload_register('my_autoloader_function');
    // [...] (even in other files)
    $obj = new Test5;
    $obj->funcZalabri();
    

    The registered autoloader function will be called each time a class is instantiated/accesses while it does not exists. In the example it’s assumed that there is one class per file which is quite common and in my opinion very nice to manage the code.

    As the autoloader is a callback and it’s written in PHP you can do whatever you want in it and make it much more detailed to fit your needs.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which looks something like this: public class Test { private
I have a class which is marked with a custom attribute, like this: public
This a continuation of this question. I have an Address class which contains basic
The problem: I have a class which contains a template method execute which calls
I have a MATLAB class which contains a reference to a java object classdef
I have a base class Node which contains a list of child nodes. Node
Let's say you have a class called Customer, which contains the following fields: UserName
I have an solution in VS 2008 which contains two class library projects and
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class 'Vector3' which is compiled successfully. It contains both non-friend and

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.