Asked  7 Months ago    Answers:  5   Viewed   30 times

I've just got an error.

When I try to assign an object like this:

$obj_md = new MDB2();

The error I get is "Assigning the return value of new by reference is deprecated". Actually I've been looking for a solution but the only one I've seen is just turn down the politicy of php.ini (error_reporting). I've tried it too, but it didn't work.

It's so confusing..I hope you could help me. Thanks in advance.

 Answers

89

In PHP5 this idiom is deprecated

$obj_md =& new MDB2();

You sure you've not missed an ampersand in your sample code? That would generate the warning you state, but it is not required and can be removed.

To see why this idiom was used in PHP4, see this manual page (note that PHP4 is long dead and this link is to an archived version of the relevant page)

Wednesday, March 31, 2021
 
madphp
answered 7 Months ago
20

Your feeling is right. It will generally work fine, but there are edge cases where it doesn't.

Using =& has these differences with =:

  • =& will try to make the right side yield a reference; = won't -- even if the right side is able to yield a reference, like a function which returns by reference.
  • =& will break the old reference set and put the left and the right side both in a new one, while = will change the value of all the elements in the same reference set as the left side to the value of the right side.

The first difference and half of the second is irrelevant in this case. After the assignment, there will be only one variable with the value of the new object*, and single-element reference sets make no sense. However, the fact that =& breaks the previous reference set is significant:

<?php

$g = 4;
$v =& $g;
$v = new stdclass();
var_dump($g); // object(stdClass)#1 (0) { }

$g = 4;
$v =& $g;
$v =& new stdclass();
var_dump($g); // int(4)

* Unless maybe the constructor leaks a reference, but even if it leaks, $this inside the constructor may be a different variable, even if it points to the same object. So I doubt one could observe behavior difference due to this.

Wednesday, March 31, 2021
 
tdous
answered 7 Months ago
21

PHPUnit_Util_Skeleton_Test is available in PHPUnit 3.6, see https://github.com/sebastianbergmann/phpunit/blob/3.6/PHPUnit/Util/Skeleton/Test.php

I suggest uninstalling all phpunit packages and reinstalling phpunit 3.6 with

$ pear install phpunit/phpunit-3.6.12

It might be that dependencies were broken when downgrading the single phpunit package with --force, and this should fix it.

Saturday, May 29, 2021
 
SJain
answered 5 Months ago
51

Ok, i somehow managed this. I looked to python documentation, and i learnt that: with using Queue class, we can get return values from a function. And final version of my code is like this:

from multiprocessing import Process, Queue

def multiply(a,b,que): #add a argument to function for assigning a queue
    que.put(a*b) #we're putting return value into queue

if __name__ == '__main__':
    queue1 = Queue() #create a queue object
    p = Process(target= multiply, args= (5,4,queue1)) #we're setting 3rd argument to queue1
    p.start()
    print(queue1.get()) #and we're getting return value: 20
    p.join()
    print("ok.")

And there is also a pipe() function, i think we can use pipe() function,too. But Queue worked for me, now.

Monday, August 2, 2021
 
Shoarc
answered 3 Months ago
21

When the validation on the FormRequest fails, it checks to see if the request was ajax or if it accepts a json response. If so, it will return a json response with the 422 status code. If not, it will return a redirect to a specified url (previous, by default). So, in order to get the response on failure you're looking for (422), you need to make a json request or an ajax request.

JSON

To make a json request, you should use the json() method:

//post exam
$this->json('POST', 'modul/foo/exam', [
        'date' => '2016-01-01'
    ])
    ->assertResponseStatus(200);

//post exam again
$this->json('POST', 'modul/foo/exam', [
        'date' => 'some invalid date'
    ])
    ->assertResponseStatus(422);

There are also getJson(), postJson(), putJson(), patchJson(), and deleteJson() shortcut methods if you think that looks cleaner than passing the method as a parameter.

//post exam
$this->postJson('modul/foo/exam', [
        'date' => '2016-01-01'
    ])
    ->assertResponseStatus(200);

AJAX

To make an ajax request, you need to add in the ajax headers. For this, you can continue to use the post() method:

//post exam
$this->post('modul/foo/exam', [
        'date' => '2016-01-01'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(200);

//post exam again
$this->post('modul/foo/exam', [
        'date' => 'some invalid date'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(422);
Wednesday, August 11, 2021
 
Elxx
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share