设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 113|回复: 0

PHP Struct class

[复制链接]
发表于 2013-11-10 12:09:20 | 显示全部楼层 |阅读模式
                                        PHP Struct class    This class gives you the ability to easily create (emulate) structs in php.
  1. <?php
  2. class Struct
  3. {
  4.     /**
  5. * Define a new struct object, a blueprint object with only empty properties.
  6. */
  7.     public static function factory()
  8.     {
  9.         $struct = new self;
  10.         foreach (func_get_args() as $value) {
  11.             $struct->$value = null;
  12.         }
  13.         return $struct;
  14.     }

  15.     /**
  16. * Create a new variable of the struct type $this.
  17. */
  18.     public function create()
  19.     {
  20.         // Clone the empty blueprint-struct ($this) into the new data $struct.
  21.         $struct = clone $this;

  22.         // Populate the new struct.
  23.         $properties = array_keys((array) $struct);
  24.         foreach (func_get_args() as $key => $value) {
  25.             if (!is_null($value)) {
  26.                 $struct->$properties[$key] = $value;
  27.             }
  28.         }

  29.         // Return the populated struct.
  30.         return $struct;
  31.     }
  32. }
复制代码


How to usePlace the class in your framework's library or something and include it. Do not put it in the same source file as the code that makes use of it. Eww.
Example 1 - Geographic coordinates
  1. <?php
  2. require 'Struct.php';

  3. // define a 'coordinates' struct with 3 properties
  4. $coords = Struct::factory('degree', 'minute', 'second', 'pole');

  5. // create 2 latitude/longitude numbers
  6. $lat = $coords->create(35, 41, 5.4816, 'N');
  7. $lng = $coords->create(139, 45, 56.6958, 'E');

  8. // use the different values by name
  9. echo $lat->degree . '° ' . $lat->minute . "' " . $lat->second . '" ' . $lat->pole;
复制代码

Example 2 - Simple struct with a 'null' value
  1. <?php
  2. require 'Struct.php';

  3. // define a struct
  4. $struct1 = Struct::factory('var1', 'var2');

  5. // create 2 variables of the 'struct1' type
  6. $instance0 = $struct1->create('val0-1', 'val0-2');
  7. $instance1 = $struct1->create('val1-1', 'val1-2');
  8. $instance2 = $struct1->create('val2-1'); // var2 will be null

  9. // use the variables later on in a readable manner
  10. echo $instance1->var2;
复制代码

Example 3 - Simple struct with a default value
  1. <?php
  2. require 'Struct.php';

  3. // define a struct with a default value
  4. $struct2 = Struct::factory('var3', 'var4');
  5. $struct2->var3 = 'default';

  6. // create 2 variables of the 'struct2' type
  7. $instance3 = $struct2->create('val3-1', 'val3-2');
  8. $instance4 = $struct2->create('val4-1', 'val4-2');
  9. $instance5 = $struct2->create(null, 'val5-2'); // null becomes the default value

  10. // use the variables later on in a readable manner
  11. echo $instance4->var3;
复制代码


FeedbackOhh boy. There has been some muppet giving me negative feedback on my implementation, however he did so without argumentation. I feel I need to clarify the need for a struct.
Constructive feedback is welcome, on twitter for example. I'm not interested in people telling me I don't get OOP. Make sure you know what you are talking about. Also, feedback comes in various forms, interface and implementation are 2 different things, make sure you state clearly what you mean.
And I'll admit, declaring the methods final was a bit much, I removed it.

When to use
  • When you need a kind of 'template' class to build structs in php.
  • When you have little data, and it feels like it belongs together logically.
  • When an array gives to you too much duplicate code. When using a array in example 1, you have to specify the key names multiple times, or use (magical) numbered keys.
  • When you don't want to implement a entire class. Because the described data is too simple/small for example.
  • When you want data format definition code and data processing code together, because it does not make sense when pulled apart. In PHP you cannot nest classes, hence the factory method.
  • When you don't want to repeat yourself.

When not to use
  • When the data belongs in a database. Use Database Models.
  • When the data comes with functionality, create your own object.
  • When the data is used on multiple places/source files in your code.
  • When you're not using OOP, just use an array.

Bran van



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 22:29 , Processed in 0.014033 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表