Echo Return construct method;

后端 未结 6 1738
有刺的猬
有刺的猬 2021-01-25 08:11

相关标签:
6条回答
  • 2021-01-25 08:42

    You could use the __toString magic method to get __construct to echo out or you could just getmetod without using __toString magic method. There are many ways just pick one of them.

    <?php
    // Declare a simple class
    class TestClass
    {
        public $foo;
    
        public function __construct($foo)
        {
            $this->foo = $foo;
        }
    
        public function __toString()
        {
            return $this->foo;
        }
    }
    
    $class = new TestClass('Hello');
    echo $class;
    ?>
    

    __toString()

    0 讨论(0)
  • 2021-01-25 08:47
    $db = new DBFactory();  
    

    i think this "()" also be here

    0 讨论(0)
  • 2021-01-25 08:49

    The constructors should not return anything.

    If you want to echo an object, you have to define how to form its string representation with the magic method __tostring:

    class DBFactory {  
         function __tostring(){  
             return 'Need to echo';
         }  
    }
    $db = new DBFactory();
    echo $db;
    
    0 讨论(0)
  • 2021-01-25 08:51

    You cannot return anything from the constructor. You're already getting a new object back, you can't get another value on top of that and assign both to $db.

    0 讨论(0)
  • 2021-01-25 08:52

    Generally it's not possible to return a value in a constructor of a class. In this case, $db contains the instance of the class, not the return value.

    You could build a separate function, and have that function return the value:

    <?php
    class DBFactory {  
          function toEcho() {
            return 'Need to echo';
          }
    }  
    $db = new DBFactory();  
    echo $db->toEcho();
    ?>
    
    0 讨论(0)
  • 2021-01-25 08:58

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

    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.

    __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.

    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.

    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();
    
    0 讨论(0)
提交回复
热议问题