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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:48:37+00:00 2026-06-12T01:48:37+00:00

really simple one I’m guessing but why does the below not work? I’m guessing

  • 0

really simple one I’m guessing but why does the below not work? I’m guessing it’s a scope thing where class2 isn’t visible from within class1. Yes, I’m getting “Call to member function on a non-object” error.

class class1 {
    function func1() {
        $class2->func3();
    }
    function func2() {
        $this->func1();
    }
}

class class2 {
    function func3() {
        echo "hello!";
    }
}

$class1 = new class1();
$class2 = new class2();

$class1->func1;

If anyone can give me a fix for this I’d be very grateful. I’ve been searching around for a while but I’m getting lots of examples of others trying to instantiate new classes inside other classes and similar and not this particular problem I have.

You’d be right in thinking I don’t do a whole lot with classes!

  • 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-12T01:48:38+00:00Added an answer on June 12, 2026 at 1:48 am

    PHP does not bubblescope like JavaScript does, so your $class2 is undefined:

    The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well […] Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

    Source: http://php.net/manual/en/language.variables.scope.php

    In other words, there is only the global scope and function/method scope in PHP. So, either pass the $class2 instance into the method as a collaborator

    class class1 
    {
        function func1($class2) {
            $class2->func3();
        }
    }
    
    $class1 = new class1();
    $class2 = new class2();
    $class1->func1($class2);
    

    or inject it via the constructor:

    class class1 
    {
        private $class2;        
    
        public function __construct(Class2 $class2)
        {
            $this->class2 = $class2;
        }
    
        function func1() 
        {
            $this->class2->func3();
        }
    }
    
    $class2 = new class2();
    $class1 = new class1($class2);
    $class1->func1();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a really simple one, but just cant get it to work.
I'm sooo... close but am stuck on one tiny thing that's probably really simple
This is probably a really simple question but one I've never quite worked out
It's really simple, I just want one screen. Wow, that [shiny thing] must have
Really simple but just not getting it. I want to reference a view by
This is probably a really simple one but I couldn't find the answer. I
This is a really simple one but it's driving me crazy. I want to
My problem is really simple but I'm not sure if there's a native solution
I suppose this one is really simple, but I can't figure out what's the
This might be a really simple thing but i was unable to find an

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.