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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:10:29+00:00 2026-06-15T14:10:29+00:00

How do you make a class property available to the other included file inside

  • 0

How do you make a class property available to the other included file inside the same class’ method?

// file A.php
class A
{
    private $var = 1;

    public function go()
    {
        include('another.php');
    }
}

in another file:

// this is another.php file
// how can I access class A->var?
echo $var; // this can't be right

Is this possible given the scope. If var is an array then we can use extract but if var is not, we can wrap it in an array. Is there a better way?

Thanks!

EDIT

okay, to clarify another.php is literally another file. Basically, in the above examples, we have 2 files A.php which contains class A and another.php which is another file/script executing something.

Answered: My bad… I included another.php from index.php.. I see scoping still applies.. thanks everyone..

  • 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-15T14:10:30+00:00Added an answer on June 15, 2026 at 2:10 pm

    Your question seems to be, “when inside a file included from within a method, how do I access a private instance member?” Right?

    In your example code, you’re including a file inside a method.

    Methods are just functions. Like all other areas of PHP, the file that gets included will inherit the entire current scope. That means that the include sees everything in scope in that method. Including $this.

    In other words, you’d access the property in the include file just like you’d access it from inside the function itself, as $this->var.


    Example, using the PHP interactive shell:

    [charles@lobotomy /tmp]$ cat test.php
    <?php
    echo $this->var, "\n";
    
    [charles@lobotomy /tmp]$ php -a
    Interactive shell
    
    php > class Test2 { private $var; public function __construct($x) { $this->var = $x; } public function go() { include './test.php'; } }
    php > $t = new Test2('Hello, world!');
    php > $t->go();
    Hello, world!
    php > exit
    [charles@lobotomy /tmp]$ php --version
    PHP 5.4.4 (cli) (built: Jun 14 2012 18:31:18)
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
        with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this CSS. the property FILTER In class .crumb make my background and
i make one class file for jar. and this class have use wurfl. and
I make a class level method for Alert: @interface TestAlert @end + (void)showErrorAlert:(NSTimer *)message
I have Class1 with a read-only bindable property called age : public class Class1
I have a server side class which I make available on the client side
To make a class immutable what I can do is: 1)Make class final 2)do
How can I make my class library STA for use with CreateObject in VBScript?
Can we make a class copy constructor virtual in C++? How to use?
I am trying to make a class that will represent two different behaivours at
I would like to make abstract class which has several inner classes extend it

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.