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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:41+00:00 2026-05-26T04:25:41+00:00

Since PHP doesn’t provide real arrays, I want to create an array class myself.

  • 0

Since PHP doesn’t provide real arrays, I want to create an array class myself. However, preferably I don’t want to create some sort of wrapper class for the PHP array, but I really want to create a real array, like in C/C++. However, if I don’t want to use the build in PHP array, I need to have some way to allocate memory manually, and to find the size of an object/class. So I need a PHP version of the c functions sizeof and malloc. Unfortunately, I couldn’t find those in PHP. Are there functions which do the same thing, or do you know of any other way to do this?

Edit: I think my question isn’t clear enough:
In PHP an array is really an order map. In some situations this can make things a little more complicated: for example, if you have an array with indices 0,1,2 and 3, you can’t relay on that you will get those items in that order in a foreach loop: it depends on the order in which you defined the items in the array. Since those little things annoy me, I want to create a ‘real’ array class, based on arrays like they are in c/c++. That means that the arrays should have a fixed size, and that I need to allocate memory manually when an array is initialized. For example, when an array of 10 integers is created, the array class should allocate a memory block of the size 10*sizeof(integer). Of course I could use the build in PHP ‘array’ to store these 10 integers, but than I’ve just created a wrapper class for the build in array type. So preferably I want to be able to allocate that memory myself so I can manage it in a better way that the build in array type does. However, I couldn’t find a function which gives me the size of a class/object, and I could find a function which could allocate memory manually, like malloc does in c.
So my question is: are there any functions for this? And if they aren’t, do you have any other efficient ways to achieve my goal?

  • 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-26T04:25:42+00:00Added an answer on May 26, 2026 at 4:25 am

    PHP already has an Array Class with SplFixedArray which behaves like you want:

    $array = new SplFixedArray(3);
    $array[1] = 'foo';
    $array[0] = 'bar';
    $array[2] = 'baz';
    foreach ($array as $k => $v) {
        echo "$k => $v\n";
    }
    $array[] = 'fails';
    

    gives

    0 => bar
    1 => foo
    2 => baz
    

    and finally

    Fatal error: Uncaught exception ‘RuntimeException’ with message ‘Index invalid or out of range’

    There is a couple of additional data structures in SPL. For an overview, see

    • http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

    If you want to know how SplFixedArray is implemented in PHP, have a look at

    • http://lxr.php.net/opengrok/xref/PHP_TRUNK/ext/spl/spl_fixedarray.c

    If you want anything different, you will probably have to implement that at the C level of the language, e.g. you’d have to write your own extension.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since PHP has no custom-class type-casting, how would I go about doing the PHP
I don't know how php.ini was configured since I don't have access to it.
PHP is somewhat crippled since it doesn't have return types (yet). I need my
My site is all php pages, since it's all database stuff. However, I'm having
Since PHP is a dynamic language what's the best way of checking to see
Sessions in PHP seemed to have changed since the last time I used them,
In a language such as (since I'm working in it now) PHP, which supports
It's been a while since I've installed PHP for Windows, but every guide I've
I am writing a website with PHP. Since it will need to be accessed
Since CS3 doesn't have a web service component, as previous versions had, is there

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.