Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial foreach循环中$p[] = $MaterialType 的疑问

foreach循环中$p[] = $MaterialType 的疑问

Jun 23, 2016 pm 02:00 PM

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;
Copy after login


回复讨论(解决方案)

我在用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) ;}
Copy after login

比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。

这就好比下面这一段代码:

$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
Copy after login
Copy after login

得到的结果必然是两个12

不好意思,上面的例子错了。只有在对象的赋值中才会有这种,因为对象本质上是一个指针

终于和flex通了一个函数,谢谢楼主。

比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。

这就好比下面这一段代码:

$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
Copy after login
Copy after login

得到的结果必然是两个12

第15行:
$getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可??
你的意思是,如果只申请一次new so_getMaterialType 那么数组p中的每个类对象都是一样的值,对吧,我在跟踪过程中的确是这样的,
$p[] = $getMaterialType ; 这里,p[] 理解成数组的指针? ,指向10个类对象,对么?

 $getMaterialType = new so_getMaterialType ;
Copy after login

关键字new会生成一个内存空间,该内存的地址赋给$getMaterialType,即$getMaterialType是一个指向前面内存空间的地址。所以,对象$getMaterialType就是指针。

$p[] = $getMaterialType ;
Copy after login

因为第一种写法没有改变$getMaterialType的值,所有p[]里所有元素指向同一块内存地址,数据当然也就一样了。
而第二种写法中,每次都会生成一个新的内存空间,并把地址赋给$getMaterialType,所以p[]中元素指向的是不同的内存空间。

简单来说:
只有$getMaterialType =  ×××才会改变$getMaterialType值,$getMaterialType->属性 = ×××不会改变$getMaterialType的值

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

See all articles