search
HomeBackend DevelopmentPHP Tutorial一个让人能绕晕的逻辑

为了便于说明,我用不同的颜色自然段说明吧
这是一个关于定义数组的问题
是在一个框架中,首先在类中定义了一个整合数组的方法

/**
 * 输出模板内容的数组,其他的变量不允许从程序中直接输出到模板
 */
private static $output_value = array();
public static function output($output,$input=''){
self::getInstance();

self::$output_value[$output] = $input;
}
在实际应用中,在一个文件里,这样写的
//板块信息
$model_web_config = Model('web_config');
$web_html = $model_web_config->getWebHtml('index');
Tpl::output('web_html',$web_html);
接着在模板中这里写的

好我的问题来了:
通过上面一系列的计算获得了数组库中的数据,并把数据放在数组里了。此时在模板中的写法首先要知道这个数组叫啥名,然后通过下标配合就能获得数组中的对应值 了,这时我的疑问来了,这里为啥数组名叫$output呢?
这个名是随便起还是说有上下文的约定呢,我看了一下上文,没有这个名字的定义呢,就突然出现,所以让人目瞪口呆!何解?


我目前的理解:也许有人会说是这句Tpl::output('web_html',$web_html);话中的output说明了,我认为不是,因为这里它是一个方法名,而不是数组名!按说上面已经定义了/**
 * 输出模板内容的数组,其他的变量不允许从程序中直接输出到模板
 */
private static $output_value = array();,为啥这里不用$output_value做数组名,而是用$output来做数组名呢,


回复讨论(解决方案)

可能是在 Tpl::output 的函数里面定义了  $output 这个数组

看看你一直的提问,发现你并非初学者(有时问题的水平很高)
那么你怎么就不知道函数(方法)定义时的形式参数呢?
public static function output( $output, $input=''){
  self::getInstance();
  self::$output_value[ $output] =  $input;
}
output 方法的两个传入参数,你总得给他个名字吧?不然怎么知道谁是谁
总不能第一个参数、第二个参数这样叫吧

执行 Tpl::output('web_html',$web_html); 时
'web_html' 就对应 参数 $output 
$web_html就对应 参数 $input 

可能是output方法中定义了

$web_html = array('index'=>'xxx');output('web_html',$web_html);function output($key, $value){	global $output;	$output[$key] = $value;	return $output;}echo $output['web_html']['index'];

可能其他地方赋值了,比如:$output = $output_value;
具体要看完整的类如何写的

可能是在 Tpl::output 的函数里面定义了  $output 这个数组


我一提醒,确实里面有
//对模板变量进行赋值
$output = self::$output_value;

看看你一直的提问,发现你并非初学者(有时问题的水平很高)
那么你怎么就不知道函数(方法)定义时的形式参数呢?
public static function output( $output, $input=''){
  self::getInstance();
  self::$output_value[ $output] =  $input;
}
output 方法的两个传入参数,你总得给他个名字吧?不然怎么知道谁是谁
总不能第一个参数、第二个参数这样叫吧

执行 Tpl::output('web_html',$web_html); 时
'web_html' 就对应 参数 $output 
$web_html就对应 参数 $input 


你没有看明白我在问啥,跑题了
Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

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

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

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.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

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

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

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

See all articles

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 Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use