As of the class constructor to return an array?

from a file 1.php declare the class
include_once('2.php');
$result = new class1();
print_r($result);

in the file 2.php a class with a constructor
class class1 {
 public function __construct() {
$res['1']='1';
$res['2']='2';
return($res);
}
}

in this embodiment, the result output file 1 empty object.
using a bunch of different attempts to get the result:
file 1.php
include_once('2.php');
$result = new class1();
print_r($result->result());

file 2.php
class class1 {
 private $res;
 function __construct() {
$this->$res['1']='1';
$this->$res['2']='2';
}
 public function result() {
return($this->$res);
}
}

in this embodiment, the result array is returned, but on every line where there is $this->$res, it generates an error stating that the variable res is not declared.
how to do it properly what and the array is passed, preferably immediately with the designer, and no error occurred?
June 7th 19 at 14:45
3 answers
June 7th 19 at 14:47
Not sure, but try this:
class class1 {
 private $res;
 public function __construct() {
 $this->res[] = '1';
 $this->res[] = '2';
return($this->res);
}
}


and in the second file:
$result = new class1();
print_r($result);


And the errors you have in the second case, arise from the structures:
$this->$res - replace it as in my example above $this->res
Thank you so much! everything works as it should) - hector_Morissette commented on June 7th 19 at 14:50
If helped, then mark the solution please. - Giles_Luettgen18 commented on June 7th 19 at 14:53
but try that

For facilitating game — shooting! - Mohamed_Lind75 commented on June 7th 19 at 14:56
June 7th 19 at 14:49
The mere existence of classes does not mean that You are using OOP approach. Your code as the question itself says about not fully understanding the OOP paradigm. So better get familiar with it, read relevant articles/books, instead of what would "go" to write a bunch of similar code. Don't want to understand - then it is not worth building the project and everything connected with them
June 7th 19 at 14:51
I could not pass. Tin same colleagues )))

If you really want classes and receive data in one call use the static methods.

class MyClass
{
 //Public method to retrieve the data
 public static function getSomeData()
{
 $result = self::someAction();

 return $result;
}

 //Special method
 protected static function someAction($data = null)
{
 //Then do something
 return $result;
}
}

$result = MyClass::getSomeData(); //get the data. Without fanaticism with the designer.


No need of tin, the descendants won't forgive you ))))

I recommend reading at your leisure: click (without the Refco).

Find more questions by tags OOPPHP