<?php
class Fruit {
private $color = "red";
public function getColor() {
return $this->color;
}
public function &getColorByRef() {
return $this->color;
}
}
echo "\nTEST RUN 1:\n\n";
$fruit = new Fruit;
$color = $fruit->getColor();
echo "Fruit's color is $color\n";
$color = "green"; // does nothing, but bear with me
$color = $fruit->getColor();
echo "Fruit's color is $color\n";
echo "\nTEST RUN 2:\n\n";
$fruit = new Fruit;
$color = &$fruit->getColorByRef(); // also need to put & here
echo "Fruit's color is $color\n";
$color = "green"; // now this changes the actual property of $fruit
$x = $fruit->getColor();
echo "Fruit's color is $color\n";
<?php
/**
* This file is part of Tree
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Nicolò Martini <nicmartnic@gmail.com>
*/
namespace Tree\Node;
use Tree\Visitor\Visitor;
trait NodeTrait
{
/**
* @var mixed
*/
private $value;
/**
* parent
*
* @var NodeInterface
* @access private
*/
private $parent;
/**
* @var NodeInterface[]
*/
private $children = [];
/**
* @param mixed $value
* @param NodeInterface[] $children
*/
public function __construct($value = null, array $children = [])
{
$this->setValue($value);
if (!empty($children)) {
$this->setChildren($children);
}
}
/**
* {@inheritdoc}
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function getValue()
{
return $this->value;
}
/**
* {@inheritdoc}
*/
public function addChild(NodeInterface $child)
{
$child->setParent($this);
$this->children[] = $child;
return $this;
}
/**
* {@inheritdoc}
*/
public function removeChild(NodeInterface $child)
{
foreach ($this->children as $key => $myChild) {
if ($child == $myChild) {
unset($this->children[$key]);
}
}
$this->children = array_values($this->children);
$child->setParent(null);
return $this;
}
/**
* {@inheritdoc}
*/
public function removeAllChildren()
{
$this->setChildren([]);
return $this;
}
/**
* {@inheritdoc}
*/
public function getChildren()
{
return $this->children;
}
/**
* {@inheritdoc}
*/
public function setChildren(array $children)
{
$this->removeParentFromChildren();
$this->children = [];
foreach ($children as $child) {
$this->addChild($child);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function setParent(NodeInterface $parent = null)
{
$this->parent = $parent;
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function getAncestors()
{
$parents = [];
$node = $this;
while ($parent = $node->getParent()) {
array_unshift($parents, $parent);
$node = $parent;
}
return $parents;
}
/**
* {@inheritDoc}
*/
public function getAncestorsAndSelf()
{
return array_merge($this->getAncestors(), [$this]);
}
/**
* {@inheritdoc}
*/
public function getNeighbors()
{
$neighbors = $this->getParent()->getChildren();
$current = $this;
// Uses array_values to reset indexes after filter.
return array_values(
array_filter(
$neighbors,
function ($item) use ($current) {
return $item != $current;
}
)
);
}
/**
* {@inheritDoc}
*/
public function getNeighborsAndSelf()
{
return $this->getParent()->getChildren();
}
/**
* {@inheritDoc}
*/
public function isLeaf()
{
return count($this->children) === 0;
}
/**
* @return bool
*/
public function isRoot()
{
return $this->getParent() === null;
}
/**
* {@inheritDoc}
*/
public function isChild()
{
return $this->getParent() !== null;
}
/**
* Find the root of the node
*
* @return NodeInterface
*/
public function root()
{
$node = $this;
while ($parent = $node->getParent())
$node = $parent;
return $node;
}
/**
* Return the distance from the current node to the root.
*
* Warning, can be expensive, since each descendant is visited
*
* @return int
*/
public function getDepth()
{
if ($this->isRoot()) {
return 0;
}
return $this->getParent()->getDepth() + 1;
}
/**
* Return the height of the tree whose root is this node
*
* @return int
*/
public function getHeight()
{
if ($this->isLeaf()) {
return 0;
}
$heights = [];
foreach ($this->getChildren() as $child) {
$heights[] = $child->getHeight();
}
return max($heights) + 1;
}
/**
* Return the number of nodes in a tree
* @return int
*/
public function getSize()
{
$size = 1;
foreach ($this->getChildren() as $child) {
$size += $child->getSize();
}
return $size;
}
/**
* {@inheritdoc}
*/
public function accept(Visitor $visitor)
{
return $visitor->visit($this);
}
private function removeParentFromChildren()
{
foreach ($this->getChildren() as $child)
$child->setParent(null);
}
}
<?php
$array = [
[
['name'=>'John B'],
['age'=>30],
['sizes'=>
[
'weight'=>80,
'height'=>120
]
]
],
[
['name'=>'Marie B'],
['age'=>31],
['sizes'=>
[
'weight'=>60,
'height'=>110
]
]
],
[
['name'=>'Carl M'],
['age'=>12],
['sizes'=>
[
'weight'=>70,
'height'=>100
]
]
],
[
['name'=>'Mike N'],
['age'=>19],
['sizes'=>
[
'weight'=>70,
'height'=>150
]
]
],
[
['name'=>'Nancy N'],
['age'=>15],
['sizes'=>
[
'weight'=>60,
'height'=>150
]
]
],
[
['name'=>'Cory X'],
['age'=>15],
['sizes'=>
[
'weight'=>44,
'height'=>150
]
]
]
];
//Method1: sorting the array using the usort function and a "callback that you define"
function method1($a,$b)
{
return ($a[2]["sizes"]["weight"] <= $b[2]["sizes"]["weight"]) ? -1 : 1;
}
usort($array, "method1");
print_r($array);
//Method 2: The bubble method
$j=0;
$flag = true;
$temp=0;
while ( $flag )
{
$flag = false;
for( $j=0; $j < count($array)-1; $j++)
{
if ( $array[$j][2]["sizes"]["weight"] > $array[$j+1][2]["sizes"]["weight"] )
{
$temp = $array[$j];
//swap the two between each other
$array[$j] = $array[$j+1];
$array[$j+1]=$temp;
$flag = true; //show that a swap occurred
}
}
}
print_r($array);
//Method3: DIY
$temp = [];
foreach ($array as $key => $value)
$temp[$value[2]["sizes"]["weight"] . "oldkey" . $key] = $value; //concatenate something unique to make sure two equal weights don't overwrite each other
ksort($temp); // or ksort($temp, SORT_NATURAL); see paragraph above to understand why
$array = array_values($temp);
unset($temp);
print_r($array);
// Method 4
array_multisort(array_map(function($element) {
return $element[2]['sizes']['weight'];
}, $array), SORT_ASC, $array);
print_r($array);
<?php
namespace Model;
use Nette;
class DemandStorage extends Nette\Object
{
protected $session;
public function __construct(Nette\Http\Session $session)
{
$this->session = $session->getSection('demandForm');
}
public function flush()
{
$this->session->remove();
}
public function getRawData()
{
return isset($this->session['formData']) ? $this->session['formData'] : array();
}
public function getStepNumber()
{
return isset($this->session['step']) ? $this->session['step'] : 1;
}
public function setRawData($data)
{
$this->session['formData'] = $data;
return $this;
}
public function addRawData($data)
{
$originalData = $this->getRawData();
$data = array_merge($originalData, $data);
$this->setRawData($data);
return $this;
}
public function setStepNumber($step = 1)
{
$this->session['step'] = $step;
return $this;
}
public function getValue($name, $default = NULL)
{
$data = $this->getRawData();
return Nette\Utils\Arrays::get($data, $name, $default);
}
public function setValue($name, $value)
{
$data = $this->getRawData();
$data[$name] = $value;
$this->setRawData($data);
return $this;
}
public function unsetValue($name)
{
$data = $this->getRawData();
if(isset($data[$name]))
unset($data[$name]);
$this->setRawData($data);
return $this;
}
public function issetValue($name)
{
$data = $this->getRawData();
return isset($data[$name]);
}
}
<?php
call_user_func(function() {
// your code here ...
});
<?php
public function checkDatum(\DateTime $date)
{
$startDate = new \DateTime('2018-12-17 00:00:00');
$endDate = new \DateTime('2018-12-23 23:59:59');
return $date > $startDate && $date < $endDate;
}
// Create a DOMDocument
$dom = new DOMDocument();
// Load html including utf8, like Hebrew
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
// Create the div wrapper
$div = $dom->createElement('div');
$div->setAttribute('class', 'responsive-img');
// Get all the images
$images = $dom->getElementsByTagName('img');
// Loop the images
foreach ($images as $image)
{
//Clone our created div
$new_div_clone = $div->cloneNode();
//Replace image with wrapper div
$image->parentNode->replaceChild($new_div_clone,$image);
//Append image to wrapper div
$new_div_clone->appendChild($image);
}
// Save the HTML
$html = $dom->saveHTML();
return $html;