1

I'm quite new to PHP (coming from a rich C++ background). It's a good language, but I'm missing some of my C++ creature comforts, like structs. So I decided to make something that would act as a struct. I came up with the following:

class Struct
{
  public string $Foo;
  public string $Bar;
}

This looks great in my opinion. Now I would like to be able to initialize these struct variables in place like how you would be able to do in C++:

Struct Structure{ "One", "Two" }

Yet, to my understanding, the only way to do this in PHP is by creating a custom constructor. One downside to this though is that you'd need to create a custom constructor for every struct you make. I think that you may be able to overcome this issue by using inheritance. But the question is: how? I was thinking of creating a constructor in the base class that can somehow initialize the variables of the derived class, but I'm not entirely sure how I would go about doing that (not to mention if that would be a 'good' solution).

Any help would be appreciated!

2
  • 1
    You can create a stdclass instance with properties using something like $struct = (object) ['Foo' => 'One', 'Bar' => 'Two']; Commented Aug 24, 2020 at 3:47
  • 1
    The all-mighty PHP data structure is the array. A PHP array is a combination of array, double-linked list and hash/map/dictionary (however you want to name it). This last one capability is usually used to simulate a struct (but the language does not provide any help to prevent using incorrect names for the "fields"). Commented Aug 24, 2020 at 7:49

4 Answers 4

4

In PHP 8.0, there is a new feature called Constructor PromotionRFC. You create a constructor along with its parameters, and at the same time, you define class properties:

class Struct
{
  public function __construct(
    public string $Foo,
    public string $Bar,
  ) {
  }
}

Cool for a C++ developer! Now, you can instantiate it easily:

$structure = new Struct("One", "Two");
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, exactly what I was looking for!
1

I will add yet another solution which may or may not satisfy your needs. You can consider one of these solutions:

class Struct
{
  public string $Foo;
  public string $Bar;

  public static function fromAssoc(array $assoc): self
  {
      $instance = new self();
      $instance->Foo = $assoc['Foo'];
      $instance->Bar = $assoc['Bar'];
      return $instance;
  }

  public static function fromArray(array $array): self
  {
      $instance = new self();
      $instance->Foo = $assoc[0];
      $instance->Bar = $assoc[1];
      return $instance;
  }
}

$struct = Struct::fromAssoc([
  'Foo' => 'One',
  'Bar' => 'Two',
]);

$struct = Struct::fromArray(['One', 'Two']);

Pick your favourite

1 Comment

This would be a good solution if it weren't for the fact that to my knowledge you wouldn't be able to divide it into a parent class and derived classes. Reason I want to do this is to avoid having to copy the same logic to each and every struct I make.
-1

Try to do this. It may help you.

class Struct{
  public $foo;
  public $bar;
}

$obj = new MyStruct();
$obj->foo = 'One';
$obj->bar = 'Two';

1 Comment

I have considered doing this, however I wasn't happy with it seeing that I wouldn't be able to initialize it in one line like the aforementioned C++ style brace initialization
-1

It sounds like you are looking for a one-liner. An object without a class and a quick way to instantiate it.

The canonical way to do this in PHP is probably not an object, it's an array:

$foo = [
  'Foo' => 'One',
  'Bar' => 'Two'
];

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.