search
HomeBackend DevelopmentPHP Tutorial种里引用另一个类里的方法,怎以不行

类里引用另一个类里的方法,怎以不行

本帖最后由 xiachao2008 于 2011-12-16 15:34:37 编辑 有一个数据库类 sql.php
有一个配置文件config.php,里面new了数据连接类得到类名$db
在一个操作文件aa.php里面引入config.php

在aa.php里,定主了一个类名Aa 我在Aa里要用数据库类sql.php里的一个方法,但是eclipse不提示方法(很明显引用不成功)
但是,我在Aa类的外面引用方法      eclipse提示了方法


这个为什么呢,难到在一个类里的方法不能引用另一个类的方法?


------解决方案--------------------
可以!
你可以在外部实例化,作为参数传递到类中
你可以在外部实例化,在类中声明全局变量
你可以在类中实例化
你还可以通过累的静态属性传递进来
你还可以采用单例模式使用它

------解决方案--------------------
引用:
有一个数据库类 sql.php
有一个配置文件config.php,里面new了数据连接类得到类名$db
在一个操作文件aa.php里面引入config.php

在aa.php里,定主了一个类名Aa 我在Aa里要用数据库类sql.php里的一个方法,但是eclipse不提示方法(很明显引用不成功) 但是,我在Aa类的外面引用方法      eclipse提示了方法


这个为什么……


如果运行有错的话,你还是吧代码贴出来看看。
------解决方案--------------------
引用:
config.php里我实例化了$db
我在aa.php引用了config.php
按上面所说的,我在Aa类里直接
$db->方法
就可以啊。
但是,实际上是不行的。

为什么可以呢?那个 $db 对于 Aa 的方法来说是外部变量,你不拿进来怎么能用呢?
------解决方案--------------------
那就要怪 eclipse 了,他居然不能识别运行时刻加载的对象!?
真该千刀万剐
------解决方案--------------------
后面我在Aa类的一个方法里引入config.php文件
但是还是不能用啊?

在该方法中应该可以用了,其他方法不可以用。
------解决方案--------------------
引用:
后面我在Aa类的一个方法里引入config.php文件
但是还是不能用啊?

在该方法中应该可以用了,其他方法不可以用。


? 你是xxx的马甲? xxx是你的马甲?


------解决方案--------------------
很神奇啊。

引用:
引用:

后面我在Aa类的一个方法里引入config.php文件
但是还是不能用啊?

在该方法中应该可以用了,其他方法不可以用。


? 你是xxx的马甲? xxx是你的马甲?
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

MinGW - Minimalist GNU for Windows

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool