class Editor{ var $Error = '' ; var $Pages; function vol() { $Pages = array(); $this->Pages[] = new page; }}class Page{ var $Id = ''; var $BackImage; function vol() { $BackImage = array(); $this->$BackImage[] = new Decorator; }}class Decorator{ var $Ext = '';}_________________________________________________________//我在使用这个类的时候$Editor = new Editor;$Editor->vol();$Editor->Pages[0]->vol(); //对象定义完成?我只初始化了Pages[0]下的class Decorator对象,为什么下面我可以直接使用page[2]和所有page数组其他元素中的class Decorator ,而不需要我$Editor->Pages[2]->vol()呢?$Editor->Pages[0]->BackImage[0]->Ext = 'str' ;$Editor->Pages[2]->BackImage[2]->Ext = 'str2' ;$Editor->Pages[5]->BackImage[4]->Ext = 'str6' ; //这样使用没有报错
回复讨论(解决方案)
这个应该是会有 警告的
class Editor{ var $Error = '' ; var $Pages; function vol() { $Pages = array(); $this->Pages[] = new page; }} class Page{ var $Id = ''; var $BackImage; function vol() { $BackImage = array();// $this->$BackImage[] = new Decorator; //Fatal error: Cannot use [] for reading $this->BackImage[] = new Decorator; //只能这样写,因为 $BackImage 是数组 }} class Decorator{ var $Ext = '';} $Editor = new Editor;$Editor->vol();$Editor->Pages[0]->vol(); $Editor->Pages[0]->BackImage[0]->Ext = 'str' ;$Editor->Pages[2]->BackImage[2]->Ext = 'str2'; //Warning: Creating default object from empty value
连语法检查都通不过!
我来试试
[ color=#993300]感觉找到原因了:[/color]
//我在使用这个类的时候
$Editor = new Editor;
$Editor->vol();
$Editor->Pages[0]->vol(); //对象定义完成?我只初始化了Pages[0]下的class Decorator对象,
//为什么下面我可以直接使用page[2]和所有page数组其他元素中的class Decorator ,
//而不需要我$Editor->Pages[2]->vol()呢?
//$Editor->name = "xlc";
echo "
";
$Editor->Pages[10]->BackImage[10]->Name = 'str' ;
echo $Editor->Pages[10]->BackImage[10]->Name;
var_dump($Editor);
输出内容为:
strobject(Editor)#1 (2) { ["Error"]=> string(0) "" ["Pages"]=> array(2) { [0]=> object(Page)#2 (2) { ["Id"]=> string(0) "" ["BackImage"]=> array(1) { [0]=> object(Decorator)#3 (1) { ["Ext"]=> string(0) "" } } } [10]=> object(stdClass)#4 (1) { ["BackImage"]=> array(1) { [10]=> object(stdClass)#5 (1) { ["Name"]=> string(3) "str" } } } } }
请查数据Pages[10]的键值,其实他创建的是一人stdClass类页并不是Page类的一个实例,这个应该是php5特性吧。
刚开始以为是魔术方法里面问题,但后来确定了一下,不是的。
没有报错应该是你的php设置了报告级别
{
$BackImage = array(1,2,3,4);
$this->BackImage[] = new Decorator;
//这里增加两句或者可帮助你理解
print_r($BackImage);
print_r($this->BackImage);
}
你需要了解一下类的属性和类内出现的一般变量的异同
建议你应该去学习 类的继承,能理清并简化你上面的思路,减少对象链、方法链的使用
谢谢楼上各位,有你们真好!再次感谢!!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
