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

I have some class /library/QPF/Loader.php namespace QPF; class Loader { protected static $loader =
What's the explanation for the following: public class GenericsTest { //statement 1 public ArrayList<Integer>[]
I have a problem with the following piece of PHP script: <?php abstract class
These are the class definitions <?php abstract class MyTemplate { protected $arrayOfSpaces; protected $arrayOfVariables;
if i have a .net 1.1 class that inherits from DictionaryBase and i am
I used to have a class in 1.1 for the Datagrid that inherited from
Is it acceptable to make more than 1 facade class (not instance) in a
I am looking for a generic, bidirectional 1 to 1 Dictionary class in C#
Is the standard Java 1.6 javax.xml.parsers.DocumentBuilder class thread safe? Is it safe to call
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass

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.