Structs data type in php?

后端 未结 7 1355
悲哀的现实
悲哀的现实 2020-12-07 17:47

Can anyone give me example for structs data type in php ? How come there is something like structs in php all of a sudden ?

相关标签:
7条回答
  • 2020-12-07 17:57

    Only associative arrays are structs in PHP.

    And you can't make them strict on their own.

    But you can sort of fake structure strictness with classes and interfaces, but beware that unlike structures, class instances are not passed in arguments, their identifiers are!


    You can define a struct through an interface (or at least close to it)

    Structs enforce a certain structure on an object.

    PHP (<= 7.3) does not have native structs, but you can get around it with interfaces and type hinting:

    interface FooStruct 
    {
        public function name() : string;
    }
    interface BarStruct 
    {
        public function id() : int;
    }
    interface MyStruct 
    {
       public function foo() : FooStruct;
       public function bar() : BarStruct;
    }
    

    Any class implementing MyStruct will be a MyStruct.

    The way it's build up is not up to the struct, it just ensures that the data returned is correct.


    What about setting data?

    Setting struct data is problematic as we end up with getters and setters and it's something that is close to an anemic object or a DTO and is considered an anti-pattern by some people

    Wrong example:

    interface FooStruct 
    {
        public function getName() : string;
        public function setName(string $value) : FooStruct;
    }
    interface BarStruct 
    {
        public function getId() : int;
        public function setId(int $value) : BarStruct;
    }
    interface MyStruct 
    {
       public function getFoo() : FooStruct;
       public function setFoo(FooStruct $value) : MyStruct;
       public function getBar() : BarStruct;
       public function setBar(BarStruct $value) : MyStruct;
    }
    

    Then we end up with class implementations that might be mutable, and a struct must not mutate, this is to make it a "data type", just like int, string. Yet there's no way to restrict that with interfaces in PHP, meaning people will be able to implement your struct interface in a class that is not a struct. Make sure to keep the instance immutable

    Also a struct may then be instantiated without the correct data and trigger errors when trying to access the data.

    An easy and reliable way to set data in a PHP struct class is through its constructor

    interface FooStruct 
    {
        public function name() : string;
    }
    interface BarStruct 
    {
        public function id() : int;
    }
    interface MyStruct 
    {
       public function foo() : FooStruct;
       public function bar() : BarStruct;
    }
    
    class Foo implements FooStruct 
    {
       protected $name;
       public function __construct(string $name)
       {
           $this->name = $name;
       }
       public function name() : string
       {
           return $this->name;
       }
    }
    
    class Bar implements BarStruct 
    {
       protected $id;
       public function __construct(string $id)
       {
           $this->id = $id;
       }
       public function id() : int
       {
           return $this->id;
       }
    }
    
    class My implements MyStruct 
    {
       protected $foo, $bar;
       public function __construct(FooStruct $foo, BarStruct $bar)
       {
           $this->foo = $foo;
           $this->bar = $bar;
       }
       public function foo() : FooStruct
       {
           return $this->foo;
       }
       public function bar() : BarStruct
       {
           return $this->bar;
       }
    }
    

    Type hinting using interfaces: (if your IDE supports it)

    If you don't mind not having the strict type checking, then another way would be using interfaces or classes with comments for the IDE.

    /**
     * Interface My
     * @property Foo $foo
     * @property Bar $bar
     */
    interface My 
    {
    
    }
    
    /**
     * Interface Foo
     * @property string|integer $id
     * @property string $name
     */
    interface Foo 
    {
    
    }
    
    /**
     * Interface Bar
     * @property integer $id
     */
    interface Bar
    {
    
    }
    

    The reason to use interfaces instead of classes is for the same reason why interfaces exist in the first place, because then many classes with many implementations can have this same structure and each method/function that uses it will support every class with this interface.

    This depends on your IDE, so you might need to use classes instead or just live without it.

    Note: Remember that you have to validate/sanitize the data in the instance elsewhere in the code to match the comment.

    0 讨论(0)
  • 2020-12-07 17:59

    You can use an array

    $something = array(
       'key' => 'value',
       'key2' => 'value2'
    );
    

    or with standard object.

    $something = new StdClass();
    
    $something->key = 'value';
    $something->key2 = 'value2';
    
    0 讨论(0)
  • 2020-12-07 18:01

    I cobbled together a 'dynamic' struct class today, had a look tonight and someone has written something similar with better handling of constructor parameters, it might be worth a look:

    http://code.activestate.com/recipes/577160-php-struct-port/

    One of the comments on this page mentions an interesting thing in PHP - apparently you're able to cast an array as an object, which lets you refer to array elements using the arrow notation, as you would with a Struct pointer in C. The comment's example was as follows:

    $z = array('foo' => 1, 'bar' => true, 'baz' => array(1,2,3));
    //accessing values as properties
    $y = (object)$z;
    echo $y->foo;
    

    I haven't tried this myself yet, but it may be that you could get the desired notation by just casting - if that's all you're after. These are of course 'dynamic' data structures, just syntactic sugar for accessing key/value pairs in a hash.

    If you're actually looking for something more statically typed, then ASpencer's answer is the droid you're looking for (as Obi-Wan might say.)

    0 讨论(0)
  • 2020-12-07 18:07

    It seems that the struct datatype is commonly used in SOAP:

    var_dump($client->__getTypes());
    
    array(52) {
      [0] =>
      string(43) "struct Bank {\n string Code;\n string Name;\n}"
    }
    

    This is not a native PHP datatype!

    It seems that the properties of the struct type referred to in SOAP can be accessed as a simple PHP stdClass object:

    $some_struct = $client->SomeMethod();
    echo 'Name: ' . $some_struct->Name;
    
    0 讨论(0)
  • 2020-12-07 18:11

    A public class is one option, if you want something more encapsulated you can use an abstract/anonymous class combination. My favorite part is that autocomplete still works (for PhpStorm) for this but I don't have a public class sitting around.

    <?php
    
    final class MyParentClass
    {
        /**
         * @return MyStruct[]
         */
        public function getData(): array
        {
            return array(
                $this->createMyObject("One", 1.0, new DateTime("now")),
                $this->createMyObject("Two", 2.0, new DateTime("tommorow"))
            );
        }
    
        private function createMyObject(string $description, float $magnitude, DateTime $timeStamp): MyStruct
        {
            return new class(func_get_args()) extends MyStruct {
                protected function __construct(array $args)
                {
                    $this->description = $args[0];
                    $this->magnitude = $args[1];
                    $this->timeStamp = $args[2];
                }
            };
        }
    }
    
    abstract class MyStruct
    {
        public string $description;
        public float $magnitude;
        public DateTime $timeStamp;
    }
    
    0 讨论(0)
  • 2020-12-07 18:13

    I recommend 2 things. First is associative array.

    $person = Array();
    $person['name'] = "Joe";
    $person['age'] = 22;
    

    Second is classes.

    Detailed documentation here: http://php.net/manual/en/language.oop5.php

    0 讨论(0)
提交回复
热议问题