array_udiff in symfony3 - can't find function - php

I'm trying to use array_udiff
public function findFreeUsers($weekId)
{
function compare_users(User $user1, User $user2)
{
return $user1->getId() <=> $user2->getId();
}
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, "compare_users");
return $freeUsers;
}
Error
Warning: array_udiff() expects parameter 3 to be a valid callback, function 'compare_users' not found or invalid function name
I declare compare_users() in public function in repository.
It's seems like symfony can't locate compare_users function. Same thing happend, when I declare coparision function after using array_udiff or declare it between public functions in repo

It would be better if you use callable function with closure, anonymous function or pointing the method of class. If you execute the method more than ones it'll try to redeclare the function and that function already exist. Moreover, the function could be declared elsewhere and it'll cause similar problem. That is why it's better to use the following solutions.
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, function (User $user1, User $user2) {
return $user1->getId() <=> $user2->getId();
});
return $freeUsers;
}
Other options
with closure:
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$compareClosure = function (User $user1, User $user2) {
return $user1->getId() <=> $user2->getId();
};
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, $compareClosure);
return $freeUsers;
}
With class callable
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, [$this, 'compare_users']);
return $freeUsers;
}
public static function compare_users(User $user1, User $user2)
{
return $user1->getId() <=> $user2->getId();
}

Related

PHP Binary Tree Recursive Traversal Infinite Loop Issue

I have a binary tree and node class that can create nodes and then recursively traverse the root for pre, post and in-order node orders. This code works when in JS, but for some reason infinitely loops with a warning of "Cannot use '$this' in non-object context." when returning $this in the addSide() function. What is causing this infinite loop, and how can I fix it?
<?php
class Node {
public $value;
public $right = null;
public $left = null;
function __constructor($value) {
$this->value = $value;
}
}
class BinaryTree {
public $root;
function __constructor() {}
function create($value) {
$newNode = new Node($value);
if (!$this->root) {
$this->root = $newNode;
return $this; //no warning
}
$current = $this->root;
function addSide($side, $current, $newNode) {
if (!$current->$side) {
$current->$side = $newNode;
return $this; //Warning: "Cannot use '$this' in non-object context."
}
$current = $current->$side;
};
while (true) {
if ($value === $current->value) return $this;
if ($value < $current->value) addSide("left", $current, $newNode);
else addSide("right", $current, $newNode);
}
}
function preOrder() {
$visited = [];
$current = $this->root;
function traversePreOrder($node) {
array_push($visited, $node->value);
if ($node->left) traversePreOrder($node->left);
if ($node->right) traversePreOrder($node->right);
};
traversePreOrder($current);
return $visited;
}
function postOrder() {
$visited = [];
$current = $this->root;
function traversePostOrder($node) {
if ($node->left) traversePostOrder($node->left);
if ($node->right) traversePostOrder($node->right);
array_push($visited, $node->value);
};
traversePostOrder($current);
return $visited;
}
function inOrder() {
$visited = [];
$current = $this->root;
function traverseInOrder($node) {
if ($node->left) traverseInOrder($node->left);
array_push($visited, $node->value);
if ($node->right) traverseInOrder($node->right);
};
traverseInOrder($current);
return $visited;
}
}
$tree = new BinaryTree();
$tree->create(50);
$tree->create(30);
$tree->create(45);
$tree->create(12);
$tree->create(29);
echo("inOrder: ". $tree->inOrder());
echo("preOrder: ". $tree->preOrder());
echo("postOrder: ". $tree->postOrder());
Since you don't seem to be from a PHP background, here are some of the things to note down:
It is __construct() and not __constructor(). This served to be a major problem in the code during value comparisons.
No need to create functions inside functions. This can lead to cannot redeclare function issues when a method is called twice.
When calling a method from another method inside a class, $this-> is necessary unless the function being called is an inbuilt function in PHP or at least available during code execution.
You seem to be creating a Binary Search Tree instead of just a Binary Tree.
Pass $visited by reference when collecting values during traversal.
You can't print arrays using echo. Use print_r() or use implode() to convert the array to string using a delimiter(say ,) and then print it using echo.
In create(), you sometimes return a node and sometimes $this. Both are not the same. Former one is an object of the Node class and the latter one is the object of the BinaryTree class.
In create() method, you simply need to traverse left or right from the current code according to the given value, which can be achieved using a simple while loop.
Corrected Code:
<?php
class Node {
public $value;
public $right = null;
public $left = null;
function __construct($value) {
$this->value = $value;
}
}
class BinaryTree {
public $root;
function __construct() {
$this->root = null;
}
function create($value) {
$newNode = new Node($value);
if ($this->root === null) {
$this->root = $newNode;
return $newNode; //no warning
}
$current = $this->root;
while($current !== null){
if($current->value > $value){
if($current->left === null){
$current->left = $newNode;
break;
}else{
$current = $current->left;
}
}else if($current->value < $value){
if($current->right === null){
$current->right = $newNode;
break;
}else{
$current = $current->right;
}
}else{
throw new \Exception("Node with $value already exists.");
}
}
return $newNode;
}
function preOrder() {
$visited = [];
$current = $this->root;
$this->traversePreOrder($current,$visited);
return $visited;
}
function traversePreOrder($node,&$visited) {
array_push($visited, $node->value);
if ($node->left !== null) $this->traversePreOrder($node->left,$visited);
if ($node->right !== null) $this->traversePreOrder($node->right,$visited);
}
function postOrder() {
$visited = [];
$current = $this->root;
$this->traversePostOrder($current,$visited);
return $visited;
}
function traversePostOrder($node,&$visited) {
if ($node->left !== null) $this->traversePostOrder($node->left,$visited);
if ($node->right !== null) $this->traversePostOrder($node->right,$visited);
array_push($visited, $node->value);
}
function inOrder() {
$visited = [];
$current = $this->root;
$this->traverseInOrder($current,$visited);
return $visited;
}
function traverseInOrder($node,&$visited) {
if ($node->left != null) $this->traverseInOrder($node->left,$visited);
array_push($visited, $node->value);
if ($node->right !== null) $this->traverseInOrder($node->right,$visited);
}
}
$tree = new BinaryTree();
$tree->create(50);
$tree->create(30);
$tree->create(45);
$tree->create(12);
$tree->create(29);
echo "inOrder: ". implode(",",$tree->inOrder()),PHP_EOL;
echo "preOrder: ". implode(",",$tree->preOrder()),PHP_EOL;
echo "postOrder: ". implode(",",$tree->postOrder()),PHP_EOL;
Online Demo

Is it possible to make a unique value in PHP?

I'm making a array class and want a value to be able to be returned by a higher order function. The idea is that its a instance constant or method returned value such that I can skip the value in a map.
In other languages making an array or some compound value, like ['skip'] will make it pointer equal such that I can then use the operator for pointer equal and it will not be equal to other arrays with the exact same content, but my problem is that ['skip'] === ['skip'] is true so even with === the two values are the same.
Here is an example of usage of my code where I accedentally have the same value as I used to skip:
namespace Test;
use Common\Domain\Collection;
$arr = new Collection();
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 4;
echo count($arr); // prints 4
$arr2 = $arr->map(function ($v) {
return $v % 2 == 0 ? Collection::SKIP : ["skip"];
});
echo count($arr2); // prints 0, but should be 2
Is there a way to get a unique value or work around this somehow?
Here is code that implements Collection:
namespace Common\Domain;;
class Collection implements \Iterator, \Countable, \ArrayAccess
{
const SKIP = ["skip"];
private $arr = [];
public function map(callable $fn, bool $keepKeys = false) :Collection
{
$arr = new static();
$nOrder = 0;
foreach($this->arr as $key => $value) {
$result = call_user_func($fn, $value, $key, $nOrder, $this);
if($result !== self::SKIP) {
if($keepKeys) {
$arr[$key] = $result;
} else {
$arr[] = $result;
}
}
}
return $arr;
}
// implementation of interfaces \Iterator, \Countable, \ArrayAccess
public function current()
{
return current($this->arr);
}
public function next()
{
next($this->arr);
}
public function key()
{
return key($this->arr);
}
public function valid()
{
return isset($this->arr[$this->key()]);
}
public function rewind()
{
reset($this->arr);
}
public function count()
{
return count($this->arr);
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->arr);
}
public function offsetGet($offset)
{
return $this->arr[$offset];
}
public function offsetSet($offset, $value)
{
$this->arr[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->arr[$offset]);
}
}
I guess you are looking for Java-type enumerations, which doesn't exist in PHP. My best guess on your problem would be to use an object instead of a constant, that you would instantiate statically for a convenient use. Then, in the loop of your map function, you check the value with an instanceof instead of the basic equality operator, against the class you defined.
So, here :
class UniqueValue
{
public static function get()
{
return new self();
}
}
Then :
$arr2 = $arr->map(function ($v) {
return $v % 2 == 0 ? UniqueValue::get() : ["skip"];
});
And inside your collection :
public function map(callable $fn, bool $keepKeys = false) :Collection
{
$arr = new static();
$nOrder = 0;
foreach($this->arr as $key => $value) {
$result = call_user_func($fn, $value, $key, $nOrder, $this);
if($result ! instanceof UniqueValue) {
if($keepKeys) {
$arr[$key] = $result;
} else {
$arr[] = $result;
}
}
}
return $arr;
}
This is the quickest approach I can think of. If your array contains data from "outside" I don't think it's possible in any way that it matches against a class check from your own code.
I would solve this by implementing another method for this. The method delete would map a function over the collection and remove any elements where the function returns false.
e.g.
class Collection
{
// ...
public function delete($func)
{
$result = new static();
foreach($this->arr as $item)
{
if($func($item) !== false) $result[] = $item;
}
}
}
// example
$arr = new Collection();
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 4;
echo count($arr); // prints 4
$arr2 = $arr->delete(function ($v) {
return $v % 2 ? true : false;
});
var_dump($arr2); // prints [2, 4]

PHP: Pass an array by reference in a class and use it in other methods

I've the following snippet:
public function __construct($s, $e, &$v, &$rv, $needle, $parser, $id) {
$this->start = $s;
$this->end = $e;
$this->vett = $v;
$this->resVett = &$rv;
$this->id = $id;
$this->needle = $needle;
$this->parser = $parser;
}
public function pushResult($result) {
$this->resVett[] = $result;
}
When I call pushResult the value is properly inserted but the original array, the one referenced by rv, is not affected.
Have you any idea of what could be the problem?
maybe the problem relies elsewhere. I checked your code with an easy example
<?php
class Example {
public $value = [];
public $reference = [];
public function __construct($value, &$reference) {
$this->value = $value;
$this->reference =& $reference;
}
public function pushResult($result) {
$this->reference[] = $result;
$this->value[] = $result;
}
}
$value = ['test'];
$reference = ['test'];
$example = new Example($value, $reference);
$example->pushResult('result');
echo 'Is Value variable same?';
var_dump($value === $example->value);
echo 'Is Reference variable same?';
var_dump($reference === $example->reference);
and passing the reference like you do it should work

Php: turning it into a recursive function

I have currently two classes.
the ArrayCompare class:
<?php
namespace App\Tools\RegexExtract;
class ArrayCompare
{
public function compare(Array $arrayToCompare)
{
$elementData = new ElementMetaData();
$metaData = $elementData->extract($arrayToCompare[0], [], $initial=true);
foreach ($arrayToCompare as $currentElement) {
$metaData = $elementData->extract($currentElement, $metaData);
}
return $metaData;
}
}
which uses the ElementMetaData class
<?php
/**
* A class for extracting meta data from an element.
*/
namespace App\Tools\RegexExtract;
class ElementMetaData
{
public function extract($element, $metaDataToCompare = [], $initial = false)
{
if ($initial == true) {
$this->isInteger($element) ? $returnMetaData['isInteger'] = $this->isInteger($element) : null;
$returnMetaData['length'] = $this->length($element);
}
else {
$returnMetaData=$metaDataToCompare;
if ($returnMetaData != []) {
if (isset ($returnMetaData['isInteger']) && !$this->isInteger($element)) {
unset($returnMetaData['isInteger']);
}
if (isset ($returnMetaData['length']) && $this->length($element) != $returnMetaData['length']) {
unset($returnMetaData['length']);
}
}
}
return $returnMetaData;
}
private function isInteger($element)
{
return is_int($element);
}
private function length($element)
{
return strlen($element);
}
}
the basic functionality is:
given I have an array
$arr=[1,2,3];
I want to get the "similarities" between ALL Elements. According to a an array i Predefine...so this would deliver this result:
$metaArray=['isInteger'=>true,'length'=>1];
and this would deliver just length as similarity:
$arr=[1,2,'D'];
$metaArray=['length'=>1];
While this array would deliver an empty result []
$arr=[1,2,'3D']; // result is [] since not all integers or not all of same length.
Now my solution does not use recursive functions...but I am sure it can be used somehow.
Also, I want to add more "criteria"....So "isEmailAdress", "beginswithA"....etc....and this would make my if statements a horror....so what is the best strategy/design pattern to follow here?
#deceze beat me to it by fair margin... but I'll still post my solution that works basically with the same principles.
abstract class abstractComparer
{
private $array;
private $result = true;
protected $name;
public function compareArray($array)
{
$current = null;
foreach ($array as $index => $value)
{
$this->result = $this->result && $this->compareValues($index, $current, $value);
$current = $value;
}
}
public function getResult()
{
return $this->result;
}
public function getName()
{
return $this->name;
}
public abstract function compareValues($index, $value1, $value2);
public abstract function getSuccessValue();
}
class intComparer extends abstractComparer
{
protected $name = "isInteger";
public function compareValues($index, $value1, $value2)
{
return is_int($value2);
}
public function getSuccessValue()
{
return true;
}
}
class lengthComparer extends abstractComparer
{
protected $name = "length";
protected $length = 0;
public function compareValues($index, $value1, $value2)
{
$this->length = strlen($value2);
return $index == 0 || strlen($value1) == $this->length;
}
public function getSuccessValue()
{
return $this->length;
}
}
And do the actual processing like this:
$temp = [1,2,3];
$comparers = [new intComparer(), new lengthComparer()];
$result = array();
foreach ($comparers as $comparer)
{
$comparer->compareArray($temp);
if ($comparer->getResult())
{
$result[$comparer->getName()] = $comparer->getSuccessValue();
}
}
//var_dump($result);
I don't see any need for recursion here, so I'll just make a suggestion for a design approach:
Implement each criterion as a class:
abstract class Criterion {
protected $valid = true;
abstract public function initialize($value);
abstract public function check($value);
public function isValid() {
return $this->valid;
}
}
class Length extends Criterion {
protected $length;
public function initialize($value) {
$this->length = strlen($value);
}
public function check($value) {
if ($this->length != strlen($value)) {
$this->valid = false;
}
}
}
You then make an array of all your criteria:
$criteria = [new Length, ...];
foreach ($criteria as $criterion) {
$criterion->initialize($values[0]);
}
And slowly whittle them down through your values:
foreach ($values as $value) {
foreach ($criteria as $criterion) {
$criterion->check($value);
}
}
$commonCriteria = array_filter($criteria, function (Criterion $criterion) {
return $criterion->isValid();
});

Call to a member function get_segment() error

I'm having this problem with this piece of PHP code:
class Core {
public function start()
{
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();
if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$uri->get_segment(1).".php")){
Only a snippet of the code
The best way I can explain it is that it is a class calling upon another class (uri.php) and i am getting the error: Fatal error: Call to a member function get_segment() on a non-object in /home/eeeee/public_html/private/funkyphp/funk/core/core.php on line 11 (the if($this->uri->get_segment(1) part)
I'm having this problem a lot and it is really bugging me.
the library code is:
<?php
class uri
{
private $server_path_info = '';
private $segment = array();
private $segments = 0;
public function __construct()
{
$segment_temp = array();
$this->server_path_info = preg_replace("/\?/", "", $_SERVER["PATH_INFO"]);
$segment_temp = explode("/", $this->server_path_info);
foreach ($segment_temp as $key => $seg)
{
if (!preg_match("/([a-zA-Z0-9\.\_\-]+)/", $seg) || empty($seg)) unset($segment_temp[$key]);
}
foreach ($segment_temp as $k => $value)
{
$this->segment[] = $value;
}
unset($segment_temp);
$this->segments = count($this->segment);
}
public function segment_exists($id = 0)
{
$id = (int)$id;
if (isset($this->segment[$id])) return true;
else return false;
}
public function get_segment($id = 0)
{
$id--;
$id = (int)$id;
if ($this->segment_exists($id) === true) return $this->segment[$id];
else return false;
}
}
?>
your calls to get_segment() are inconsistent.
In one case you call $this->uri->get_segment(), which is correct according to your previous code. The second time you call $uri->get_segment, which is missing the $this-> and so is not a valid object.

Categories