foreach循环中$p[] = $MaterialType 的疑问
class so_getMaterialType{ var $CreateTime = ''; var $IsDefault = ''; var $UpdateTime = '';}$p = array();$sql = 'SELECT * from tb where lid<50';$re_materialTypeID = $db->getAll($sql);foreach($re_materialTypeID as $key => $val){ $getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可?? $getMaterialType->CreateTime = $val['CreateTime']; $getMaterialType->IsDefault = $val['IsDefault']; $getMaterialType->UpdateTime = $val['UpdateTime'] ; $p[] = $getMaterialType ;}var_dump($p);return $p; /*????????这种写法与上面差别是什么?????????????*/$getMaterialType = array();$getMaterialType[] = new so_getMaterialType ;$sql = 'SELECT * from tb where lid<50';$re_materialTypeID = $db->getAll($sql);foreach($re_materialTypeID as $key => $val){ $getMaterialType[$key]->CreateTime = $val['CreateTime']; $getMaterialType[$key]->IsDefault = $val['IsDefault']; $getMaterialType[$key]->UpdateTime = $val['UpdateTime'] ;}var_dump($getMaterialType);return $getMaterialType;
回复讨论(解决方案)
我在用amf2.1.1 与flex通讯,产生的问题,调试过程中,传递这样类的数组总是有问题
第一段代码
$p 是一个含有若干个 so_getMaterialType 对象的数组
第二段代码
$getMaterialType 的第一个元素是 so_getMaterialType 对象,其余的是 stdClass 对象
显然你需要的是第一种写法的结果
因此可以
class so_getMaterialType { var $CreateTime = ''; var $IsDefault = ''; var $UpdateTime = ''; function __construct($a) { foreach($a as $k->$v) $this->$k = $v; }}$p = array();$sql = 'SELECT * from tb where lid<50';$re_materialTypeID = $db->getAll($sql); foreach($re_materialTypeID as $key => $val) { $p[] = new so_getMaterialType($val) ;}
比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。
这就好比下面这一段代码:
$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
得到的结果必然是两个12
不好意思,上面的例子错了。只有在对象的赋值中才会有这种,因为对象本质上是一个指针
终于和flex通了一个函数,谢谢楼主。
比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。
这就好比下面这一段代码:
$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
得到的结果必然是两个12
第15行:
$getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可??
你的意思是,如果只申请一次new so_getMaterialType 那么数组p中的每个类对象都是一样的值,对吧,我在跟踪过程中的确是这样的,
$p[] = $getMaterialType ; 这里,p[] 理解成数组的指针? ,指向10个类对象,对么?
$getMaterialType = new so_getMaterialType ;
关键字new会生成一个内存空间,该内存的地址赋给$getMaterialType,即$getMaterialType是一个指向前面内存空间的地址。所以,对象$getMaterialType就是指针。
$p[] = $getMaterialType ;
因为第一种写法没有改变$getMaterialType的值,所有p[]里所有元素指向同一块内存地址,数据当然也就一样了。
而第二种写法中,每次都会生成一个新的内存空间,并把地址赋给$getMaterialType,所以p[]中元素指向的是不同的内存空间。
简单来说:
只有$getMaterialType = ×××才会改变$getMaterialType值,$getMaterialType->属性 = ×××不会改变$getMaterialType的值

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
