# 类与2D数组Classes vs 2D arrays

``````// Using a class
class someClass
{
public  \$name;
public  \$height;
public  \$weight;

function __construct(\$name, \$height, \$weight)
{
\$this -> name       = \$name;
\$this -> height = \$height;
\$this -> weight = \$weight;
}
}

\$classArray[1] = new someClass('Bob', 10, 20);
\$classArray[2] = new someClass('Fred', 15, 10);
\$classArray[3] = new someClass('Ned', 25, 30);

// Using a 2D array
\$normalArray[1]['name'] = 'Bob';
\$normalArray[1]['height']   = 10;
\$normalArray[1]['weight']   = 20;

\$normalArray[2]['name'] = 'Fred';
\$normalArray[2]['height']   = 15;
\$normalArray[2]['weight']   = 10;

\$normalArray[3]['name'] = 'Ned';
\$normalArray[3]['height']   = 25;
\$normalArray[3]['weight']   = 30;
``````

## 9 个回复9

### ===============>>#1 票数：9 已采纳

``````public function setWeight(\$weight)
{
if(\$weight >= 0)
{
\$this->weight = \$weight;
}
else
{
// Handle this scenario however you like
}
}
``````

### ===============>>#3 票数：4

1）如果应用程序的多个部分使用数据结构，则使其成为一个类。

2）如果您正在使用它来快速处理应用程序的一部分中的数据，请将其设为2D数组。

@Richard Varno

### ===============>>#8 票数：0

Teifion，如果你只使用类作为数组的替代品，那么你就不用OOP了。 OOP的本质是对象具有知识和责任，实际上可以做事并与其他类合作。 您的对象只具有知识，除了空闲存在之外无法做任何其他事情，但它们似乎是持久性提供程序（知道如何在数据库中存储/检索自身的对象）的良好候选者。

### ===============>>#9 票数：0

``````    <?php
\$rx = "";
\$rt = "";
\$rf = "";

\$ta = 0; // total array time
\$tc = 0; // total class time

// flip these to test different attributes
\$test_globals = true;
\$test_functions = true;
\$test_assignments = true;

// define class

class TestObject
{
public \$a;
public \$b;
public \$c;
public \$d;
public \$e;
public \$f;

public function __construct(\$a,\$b,\$c,\$d,\$e,\$f)
{
\$this->a = \$a;
\$this->b = \$b;
\$this->c = \$c;
\$this->d = \$d;
\$this->e = \$e;
\$this->f = \$f;
}

public function setAtoB()
{
\$this->a = \$this->b;
}
}

// begin test

echo "<br>test assignments: " . \$test_assignments;
echo "<br>test globals: " . \$test_globals;
echo "<br>test functions: " . \$test_functions;
echo "<br>";

for (\$z=0;\$z<10;\$z++)
{
\$starta = microtime(true);

for (\$x=0;\$x<100000;\$x++)
{
\$xr = getArray('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff');

if (\$test_assignments)
{
\$xr['e'] = "e";
\$xr['c'] = "sea biscut";
}

{
\$rt = \$x['b'];
\$rx  = \$x['f'];
}

if (\$test_functions) { setArrAtoB(\$xr); }
if (\$test_globals) { \$rf = glb_arr(); }
}
\$ta = \$ta + (microtime(true)-\$starta);
echo "<br/>Array time = " . (microtime(true)-\$starta) . "\n\n";

\$startc = microtime(true);

for (\$x=0;\$x<100000;\$x++)
{
\$xo = new TestObject('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff');

if (\$test_assignments)
{
\$xo->e = "e";
\$xo->c = "sea biscut";
}

{
\$rt = \$xo->b;
\$rx = \$xo->f;
}

if (\$test_functions) { \$xo->setAtoB(); }
if (\$test_globals) { \$xf = glb_cls(); }
}

\$tc = \$tc + (microtime(true)-\$startc);
echo "<br>Class time = " . (microtime(true)-\$startc) . "\n\n";

echo "<br>";
echo "<br>Total Array time (so far) = " . \$ta . "(100,000 iterations) \n\n";
echo "<br>Total Class time (so far) = " . \$tc . "(100,000 iterations) \n\n";
echo "<br>";

}
echo "TOTAL TIMES:";
echo "<br>";
echo "<br>Total Array time = " . \$ta . "(1,000,000 iterations) \n\n";
echo "<br>Total Class time = " . \$tc . "(1,000,000 iterations)\n\n";

// test functions

function getArray(\$a,\$b,\$c,\$d,\$e,\$f)
{
\$arr = array();
\$arr['a'] = \$a;
\$arr['b'] = \$b;
\$arr['c'] = \$c;
\$arr['d'] = \$d;
\$arr['d'] = \$e;
\$arr['d'] = \$f;
return(\$arr);
}

//-------------------------------------

function setArrAtoB(\$r)
{
\$r['a'] = \$r['b'];
}

//-------------------------------------

function glb_cls()
{
global \$xo;

\$xo->d = "ddxxdd";
return (\$xo->f);
}

//-------------------------------------

function glb_arr()
{
global \$xr;

\$xr['d'] = "ddxxdd";
return (\$xr['f']);
}

//-------------------------------------

?>
``````

1回复

1回复

4回复

4回复

2回复

1回复

3回复

3回复

1回复

2回复