Echo返回构造方法; [英] Echo Return construct method;

查看:108
本文介绍了Echo返回构造方法;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
class DBFactory {  
     function __construct(){  
         return 'Need to echo';
                  }  
}  
$db = new DBFactory;  
echo $db;
?>

无效:

推荐答案

我不明白为什么你查看OOP如果你的tryiung返回值的构造函数。

I dont understand why your looking into OOP if your tryiung to return values on a constructor.

OOP的整个点是有对象执行许多任务,如果你想返回一个字符串,数组,资源然后OOP不适合你。

the whole point of OOP is to have objects that perform many tasks, if you want to return a string,array,resource then OOP is not for you.

__构造函数用于在对象初始化,witch允许您在用户可以使用对象之前执行代码来准备对象。

__constructors are used to initiate code during the pre stages of the object initialization, witch allows you to execute code to prepare an object before the user can use it.

如果您希望对对象使用__toString,

If you wish to use the __toString on objects then use it wisely, its main perpose is for a readability factor in objects, not storage etc. mainly used in error debugging.

当你使用 new <>创建一个对象时, / code>关键字php的处理器创建一个对象并将其分配给内存,然后运行该构造,但不保留任何返回的值,在构造函数到达其endppoint之后,该对象在内存中的链接返回到您要求的变量。所以在理论上你可以运行 $ db-> __ construct()作为它的一个方法,但只有在对象完全创建后。

When you create an object using the new keyword php's processor creates an object and assigns it to the memory, it then runs the construct but does not hold any returned values from it, after the constructor as reached its endppoint, the link for the object in the memory is returned to the variable you asked it to be. so in theory you can run $db->__construct() as its still a method, but only after the object is fully created.

只需创建一个方法来返回一个像这样的字符串

just create a method to return a string like so

class DBFactory
{
     function whatAmI()
     {
         return 'I am DBFactory';
     }  
}
$MyOBJECT = new DBFactory;
echo $MyOBJECT->whatAmI();






这真的是真的愚蠢的,想知道如何,


This is REALLY REALLY Stupid to do but as you wish to know how,

class DBFactory{  
     function __construct()
     {
         return 'Need to echo';
     }
}

$db = new DBFactory();
echo $db->__construct();

这篇关于Echo返回构造方法;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆