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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:48:14+00:00 2026-06-09T00:48:14+00:00

I know this is done all the time, but for some reason, it’s not

  • 0

I know this is done all the time, but for some reason, it’s not making one bit of sense to me. [Insert @#$%! here] I don’t know what the proper OOP procedure is for this (assumingly) obvious solution. I’ve briefly read up on Abstract and Interface type classes, with no avail to any examples that do what I’m asking.

So here’s the setup…

In this example, I have two classes, one that I am going to call from my script, and another two that I could either call from my script or from within the first class that I called. The psuedo-code:

<?php

    // Some code here

    $timer = new timer();
    $doSomethingCool = new doSomethingCool();

    $doSomethingCool->doIt();
    print $timer->length();



    class timer {
        private $startTime;
        private $stopTime;
        private $length;

        public function start() {
            // Start running the timer
        }
        public function end() {
            // End the timer
        }
        public function length() {
            // Return the length of the timer
            return $this->length;
        }
    }

    class doSomethingCool {
        public function doIt() {

            $timer->start();

            // Some code here

            $timer->end();

        }
    }

?>

I have been able to get this to “run” (see below), but this work-around is messy, and I am 100% sure this isn’t proper object oriented modeling:

<?php
    // Start by declaring all classes and pass all classes to themselves...

    $doSomethingCool = new doSomethingCool();
    $timer = new timer();
    $class = array(
        'doSomethingCool'=>$doSomethingCool,
        'timer'=>$timer
    );

    $doSomethingCool->class = $class;
    $timer->class = $class;


    // Some code here
    $class['doSomethingCool']->doIt();
    print $timer->length();



    class timer {
        // In each class we now declare a public variable
        // 'class' that we had passed all class instances to...
        public $class;

        private $startTime;
        private $stopTime;
        private $length;

        public function start() {
            // Start running the timer
        }
        public function end() {
            // End the timer
        }
        public function length() {
            // Return the length of the timer
            return $this->length;
        }
    }

    class doSomethingCool {
        public $class;

        public function doIt() {

            $this->class['timer']->start();

            // Some code here

            $this->class['timer']->end();

        }
    }

?>

Due to E_STRICT, I do not want to use $timer::start();.

So what’s the solution, ladies and gentlemen? Thanks!

  • 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-09T00:48:17+00:00Added an answer on June 9, 2026 at 12:48 am

    I think you want to inject other classes into some class. If so:

    class Database {}
    class Timer {}
    
    class Foo
    {
        protected $database;
        protected $timer;
    
        public function __construct(Database $database, Timer $timer)
        {
             $this->database = $database;
             $this->timer = $timer;
        }
    
        public function doSomething()
        {
            $this->timer->start();
        }
    
    }
    
    $database = new Database();
    $timer = new Timer();
    $foo = new Foo($database, $timer);
    

    Note that you should add your real classes of course (where I have used Database and Timer).

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

Sidebar

Related Questions

I know this one's been done to death, but most of the onces I've
I know this could be done manually with some hardcoded Linq Joins. However, I
I know this can be done and i have seen it done using some
I know this has been done many times before (some posts are really old
I know this question has been done but I have a slightly different twist
I know I've done this before years ago, but I can't remember the syntax,
I know this is possible to be done using javascript, and I presume I
I know we've done this before in another .aspx page that's using this master
I know that this can be easily done by using if(i%5 == 0 OR
I've done this a half dozen times so I know it's possible. I just

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.