search
HomeBackend DevelopmentPHP Tutorial 小弟我如何写一个方法,让他返回的值是一个对象

我怎么写一个方法,让他返回的值是一个对象
我怎么写一个方法,让他返回是一个对象??

------解决方案--------------------
直接return就行
------解决方案--------------------

PHP code
class myObject{
    // ......
}

function returnObject(){
    $mo = new $myObject();
    return $mo;
}
<br><font color="#e78608">------解决方案--------------------</font><br>3楼那个正确<br>如果是一个类中的成员函数的话,返回伪变量$this就是返回一个对象(它本身)
<br><font color="#e78608">------解决方案--------------------</font><br>returnObject里就new 实例化myObject类。并返回。有什么不理解的?<br><br><br><br>
探讨

可以在详细一点吗
有一点没有理解透。
引用:

PHP code
class myObject{
// ......
}

function returnObject(){
$mo = new $myObject();
return $mo;
}

------解决方案--------------------
C# code

class MyClass
{
......
}

public MyClass ReturnObject(MyClass myClass)
{
MyClass a=new MyClass();
if (null != myClass)
{
a=myClass; 
}
return a;
}
------解决方案--------------------
见4楼


探讨

是这样的。我执行了一个方法,方法本来是返回string类型的数据
而这个数据在其它地方也用得到,所以我想返回一个对象。这样在用的时候,我就可以这样写成类拟这样的写法

$a->$b()->$c()

只是觉得这样写起来更简洁。所以才有这样的问题。引用:

可以在详细一点吗
有一点没有理解透。
引用:
……

------解决方案--------------------
return $this->$a;

这个$this->$a是个什么东东?

妹子,你的基本功不扎实啊
------解决方案--------------------
探讨

class myObject{

public function a($a){

return $this->$a;
}

public function b($b){

echo $b;
}
}
$c=new myObject();

$c->a('1aaaaa')->b();

为什么不行呢,没有输出??引用 10 楼 helloyou0……

------解决方案--------------------
修正一下
例子中的b方法不用返回对象,因为他是作为终结
例子里如果b方法后再跟一个a方法的话就会出错
------解决方案--------------------
翻了30秒居然没找到合适的例子,
只好亲自动手了

class 人 {
private $name='';
private $肚子=array();

public function __construct($name){
$this->name=$name;
}

public function 吃($好吃的){
$this->肚子[]=$好吃的;
return $this;
}

public function 吐(){
$东东=array_pop($this->肚子);
if(!empty($东东)){
echo $this->name.'吐了'.$this->东东."\n";
}
return $this;
}

}

$我=new 人('helloyou0');
$我->吃('青菜')->吐()->吃('鸡')->吃('鸭')->吐()->吐();



------解决方案--------------------
你是不是要这样子?




class laaaaa{
public $b;
public function b($b){
$this -> b = $b;
echo $this -> b;
}
}

class myObject{

public $laaaaa;

public function a($a){

return $this->$a = new $a();
}


}
$c=new myObject();


$c->a('laaaaa')->b('=_=');

?>


探讨

代表返回字符串的对象啊引用:

return $this->$a;

这个$this->$a是个什么东东?

妹子,你的基本功不扎实啊
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 Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.