0

I have written down the following for the above question

<?php

class sortClass{

  public function __construct(array $arrayPassed){
    return sort($arrayPassed);
    }

}
$newarray=array(11,-2,4,35,0,8,-9);
$sorted =  new sortClass($newarray);
print_r($sorted);

?>

And the output is 'sortClass Object()' , I'd like some help to understand why it would not print out the sorted array , When we instantiate a class - the constructor will automatically execute and return what ever it returns right? so why isn;t printing $sorted object printing whats returned in the constructor?

1
  • 4
    You cannot return a value from the constructor, the return of using new ... is a new object of the class type. Commented Feb 16, 2020 at 17:33

4 Answers 4

2

You don't need a wrapper class just to sort, you can just run sort function directly, the issue with your code is, returning in __constructor.

I suggest you run it directly like this:

<?php
  $newarray = array(11,-2,4,35,0,8,-9);
  sort($newarray);
  print_r($newarray);
?>

or this one if you still need a OOP version:

<?php

class sortClass{
  private $arrayPassed = [];
  public function __construct(array $arrayPassed){
    $this->arrayPassed = $arrayPassed;
  }

  public function sort() {
    sort($this->arrayPassed);
    return $this->arrayPassed;
  }
}

$newarray = array(11,-2,4,35,0,8,-9);

$sortClass =  new sortClass($newarray);

print_r($sortClass->sort());
Sign up to request clarification or add additional context in comments.

Comments

2

https://www.php.net/manual/en/language.oop5.basic.php

You misunderstood OOP, you need to learn about it before write code. I give a workaround just for the idea:

class sortClass
{
    public $sorted;

    public function __construct(array $arrayPassed)
    {
        sort($arrayPassed);
        $this->sorted = $arrayPassed;
    }
}

$newarray = array(11, -2, 4, 35, 0, 8, -9);
$sorted = new sortClass($newarray);
var_dump($sorted->sorted);

Comments

1

You can't return anything from a constructor method. You have to create a new public method which return the sorted the value.

class MyClass
{
  private $sorted;

  public function __construct(array $input)
  {
     $this->sorted = asort($input);
  }

  public function sortedArray()
  {
     return $this->sorted;
  }
}

$arr = [11, -2, 4, 35, 0, 8, -9];
$sorted = new MyClass($arr);
print_r($sorted->sortedArray());

Comments

0

AS Nigel Ren explain you can't return from a constructor method. You need a new method to return the sorted the value. Check my code

class sortClass{
  private $sortedArray;

  public function __construct(array $arrayPassed){
    $this->sortedArray = asort($arrayPassed);
    }

  public function  getSortedArray(){
     return $this->sortedArray;
  }

}
$newarray=array(11,-2,4,35,0,8,-9);
$sorted =  new sortClass($newarray);
print_r($sorted->getSortedArray());

1 Comment

sort() method return boolean since it take argument as reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.