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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:39:17+00:00 2026-05-20T15:39:17+00:00

class_1.php class class_1 { public function __construct() { require_once $_SERVER[‘DOCUMENT_ROOT’].’/array.php’; } } class_2.php class

  • 0

class_1.php

class class_1
{
    public function __construct()
    {
        require_once $_SERVER['DOCUMENT_ROOT'].'/array.php';
    }
}

class_2.php

class class_2
{
    static function output_array()
    {
        return $array_NUMBERS;
    }
}

array.php

$array_NUMBERS = array('1', '2', '3');

page.php

require_once $_SERVER['DOCUMENT_ROOT'].'/class_1.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/class_2.php';

$obj = new class_1();

$numbers = class_2::output_array();

echo '<pre>';
print_r($numbers);

What am I doing wrong here? Aren’t you supposed to use “require_once” within classes?

Problem: It’s not outputting the array values.

  • 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-20T15:39:18+00:00Added an answer on May 20, 2026 at 3:39 pm

    You’re creating a local variable in class_1::__construct(), which falls out of scope immediately and is lost forever.

    Even if it weren’t, variables declared inside a function are local to that function, and cannot be access from other functions. class_2::output_array() has no concept of the variable $array_NUMBERS. You need to read up on scope.

    To make this work, you’d have to make the variable a public member of class_1:

    class class_1 {
        public $array_NUMBERS;
        function __construct() {
            require_once('array.php');
        }
    }
    
    class class_2 {
        public static function output_array() {
            $class1 = new class_1();
            return $class1->array_NUMBERS;
        }
    }
    

    array.php:

    $this->array_NUMBERS = array(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is dbconnect.class.php: <?php class Connect { public $error; protected $db; public function __construct()
This my dbconnect.class.php <?php class Connect { //public $error; public $db; public function __construct()
Example class code: <?php class example { public function forExample() { return true; }
What I'd like is for this class class Car { public function __construct(Engine $engine,
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
You will have to get.class.php for giving help. Working case(index.php spaghetti): <?php require_once 'classes/get.class.php';
I'm having this problem with this piece of PHP code: class Core { public
I have the following code written, <?php require_once products.php; $test = new Products; $array
I have some class /library/QPF/Loader.php namespace QPF; class Loader { protected static $loader =
define('anActionType', 1); $actionTypes = array(anActionType => 'anActionType'); class core { public $callbacks = array();

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.