4

I have this class which populates and prints an array

<?php

class testArray
{
    private $myArr;

    public function __construct() { 
        $myArr = array();
    }
    public static function PopulateArr() {

        $testA = new testArray();
        $testA->populateProtectedArr();
        return $testA;

    }
    protected function populateProtectedArr()
    {
        $this->myArr[0] = 'red'; 
        $this->myArr[1] = 'green'; 
        $this->myArr[2] = 'yellow';
        print_r ($this->myArr); 


    }
    public function printArr() {
        echo "<br> 2nd Array";
        print_r ($this->myArr);
    }
}
?>

I instantiate this class from another file and try to print the array in different function.

<?php
    require_once "testClass.php";


    $u = new testArray();
    $u->PopulateArr();
    $u->printArr();
?>

I am not able to print the array in the printArr() function. I want to get reference to the array that I had set the values in .

5
  • 3
    Your populateProtectedArr() needs to return $this->myArr Commented Jul 7, 2015 at 7:13
  • public function __construct() { $myArr = array(); } should become: public function __construct() { $this->myArr = array(); } Commented Jul 7, 2015 at 7:14
  • PopulateArr() is defined static, but you're calling it as though it was an instance method Commented Jul 7, 2015 at 7:15
  • @MarkBaker you are wrong in both cases, no need to return anything, and also static methods can be called from objects too Commented Jul 7, 2015 at 7:23
  • @GeorgeGarchagudashvili - Then perhaps you'd interpret what the poster is trying to do, and explain why PopulateArr() is defined as static? As it is, I'd already given up trying to figure out the logic of this until you reminded me by telling me I was wrong Commented Jul 7, 2015 at 7:37

3 Answers 3

1

You just missed one thing, you have to assign result of $u->PopulateArr(); to $u again, otherwise you will not get the object you created from that method call, so:

$u = new testArray();
$u = $u->PopulateArr(); // this will work
$u->printArr();

This also can be done like this:

$u = testArray::PopulateArr();
$u->printArr();
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that your $u object never populates the private array.

Instead you create a new object $testA and populate its array.

Comments

1

This might help you understanding the way

class testArray
{
    private $myArr;

    public function __construct() { 
        $this->myArr = array();
    }
    public static function PopulateArr() {

        $testA = new testArray();
        $testA->populateProtectedArr();
        return $testA;

    }
    protected function populateProtectedArr()
    {
        $this->myArr[0] = 'red'; 
        $this->myArr[1] = 'green'; 
        $this->myArr[2] = 'yellow';
        return $this->myArr;
    }
    public function printArr() {
        echo "<br> 2nd Array";
        return $this->PopulateArr();
    }
}

another.php

require_once "testClass.php";
$u = new testArray();
print_r($u->PopulateArr());
print_r($u->printArr());

Here we are accessing the values of protected function PopulateArr instead of printing within function I just replaced it with return and print it over another file and within printArr function just call the PopulateArr function and that's it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.