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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:03:30+00:00 2026-05-27T01:03:30+00:00

If I have for example the following class: class Test { private $field =

  • 0

If I have for example the following class:

class Test {
    private $field = array();

    function __construct($field) {
        $this->field = $field;
    }

    public function setField($field) {
        $this->field = $field;
    }
    public function getField() {
        return $this->field;
    }
}

And I create an instance of this class:

$simpleArray = (1, 2, 3, 4);
$simpleTest = new Test($simpleArray);

How can I then print the value of simpleArray[2] in one line? I know this solution:

$saveArray = $simpleTest->getField();
echo $saveArray[2];

I want to know how to directly access array value after getField(), so that I don’t have to save the array to variable:

echo $simpleTest->getField()->....?
  • 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-27T01:03:30+00:00Added an answer on May 27, 2026 at 1:03 am

    You could add a parameter which could serve as an index for what item to return, as can be seen below. Adding another method named something like fieldAt($index) could be another solution.

    public function getField($index = null) {
        if($index != null)
        {
            return $this->field[$index];
        }
        return $this->field;
    }
    
    $saveArray = $simpleTest->getField(2); // get the 3rd element in the array
    

    Another solution could be to implement the ArrayAccess interface, which would allow you to use the array access operators ([]) on your object:

    class Test implements ArrayAccess{
        private $field = array();
    
        function __construct($field) {
            $this->field = $field;
        }
    
        public function setField($field) {
            $this->field = $field;
        }
        public function getField() {
            return $this->field;
        }
    
        public offsetExists($offset)
        {
            return isset($this->field[$offset]);
        }
        public offsetGet($offset)
        {
            if($this->offsetExists($offset))
            {
                return $this->field[$offset];
            }
            return null;
        }
        public void offsetSet($offset, $value) { } // Can implement this method, if desired
        public void offsetUnset($offset ) { } // Can implement this method, if desired
    }
    
    $testObj[2]; // get the 3rd element in the array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data: [{class:test,description:o hai,example:a,banana:b}] As this JSON data is already in
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
Let's have a following example: public class X { } public class Y {
Take the following example, I have a class public class SomeItem { public string
Say suppose I have the following Java code. public class Example { public static
As a simplified example, I have the following data classes: public class Employee {
I have code of the following form: class Test { private final A t;
Suppose we have the following code: class Test { private Test() { System.out.println(test); }
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Suppose I have some Message class like the following. (This is a made-up class

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.