I've got a function that give the childs of a father sector. So I need to print each one and their childs, and so on.
class sector {
public $id;
public $name;
public $father_sector;
public function bringChilds() : array {
return BD::findChilds();
}
}
The function returns an array of objects or just empty.
I need to print the list of sectors.
I tried using a while, but i do not understand where to stop.
Something like:
print($sector)
foreach($sector->bringChilds() as $child) {
print($child)
foreach($child->bringChilds() as $childer) {
...
}
}
Any help? Thanks and sorry for my english.
You just need recursive calls to a function that takes $sector as an argument.
<?php
function printSectors($sector){
print($sector);
foreach($sector->bringChilds() as $child) {
printSectors($child); // call for child recursively again
}
}
printSectors($sector);
Related
class dir_exam
{
public $db_ruta;
function __construct($db_ruta)
{
$this->db_ruta=$db_ruta;
}
function veritas()
{
$aa="ok";
$xx="ok2";
return $aa;
return $xx;
}
function create_d()
{
$r=$this->veritas();
echo $r->$aa;
echo $r->$xx;
}
}
I have this class and i try execute funtion veritas inside function create_d, but i want show the value from function veritas as individual values, showing value in create_d for $aa and $xx, when execute finally the class
<?php
$a=new dir_exam("db_p");
echo $a->create_d();
?>
But i can´t get this finally, i don´t know if it´s not possible or what, this it´s my question, thank´s in advanced
You can't have 2 or more returns in a function.
For you use the vars $aa and $xx like OOP, you must create the 2 var in the class
class dir_exam
{
public $db_ruta;
public $aa; // <--
public $xx; // <--
}
After, you need change the function veritas to pass the value for your attributes
function veritas()
{
$this->aa="ok";
$this->xx="ok2";
}
Now in your function you can call like that:
function create_d()
{
$this->veritas();
echo $this->aa;
echo $this->xx;
}
Hi I am trying to build a class to emulate Gouette as a learning exercise:
https://github.com/FriendsOfPHP/Goutte/blob/master/README.rst
I think I am on the right track by using method chaining which I think they are doing, what I'm not sure of is how they do something like this:
$crawler->filter('h2 > a')->each(function ($node) {
print $node->text()."\n";
});
Would this be some kind of anonymous function?
This is my code so far:
class Bar
{
public $b;
public function __construct($a=null) {
}
public function chain1()
{
echo'chain1';
return $this;
}
public function loop($a)
{
echo'chain2';
return $this;
}
public function chain2()
{
echo'chain2';
return $this;
}
}
$a=array('bob','andy','sue','rob');
$bar1 = new Bar();
$bar1->chain1()->loop($a)->chain2();
I've tried to simplify the code to show just this one aspect of what your after...
class Bar
{
private $list;
public function __construct($a=null) {
$this->list = $a;
}
public function each( callable $fn )
{
foreach ( $this->list as $value ) {
$fn($value);
}
return $this;
}
}
$a=array('bob','andy','sue','rob');
$bar1 = (new Bar($a))->each(function ($value) {
print $value."\n";
});
As you can see, I've created the object with the list you have, and then just called each() with a callable. You can see the function just takes the passed in value and echoes it out.
Then in each() there is a loop across all the items in the list provided in the constructor and calls the closure ($fn($value);) with each value from the list.
The output from this is...
bob
andy
sue
rob
As for the chained calls, the idea is (as you've worked out) is to return an object which will be the start point for the next call. Some use $this (as you do) some systems (like Request commonly does) return a NEW copy of the object passed in. This is commonly linked to the idea of immutable objects. The idea being that you never change the original object, but you create a new object with the changes made in it. Psr7 Http Message, why immutable? gives some more insight into this.
How can I chain multiple methods together without knowing how many there will be? For instance how can I call this addMultiLink method more than once like a loop?
(new EntryField('products'))->addMultiLink($product_ids[0])
Basically I would want the result to be like this:
(new EntryField('products'))->addMultiLink($product_ids[0])->addMultiLink($product_ids[1])->addMultiLink($product_ids[2])
In your addMultiLink return $this:
public function addMultiLink($argument)
{
// your code here
return $this;
}
But as I can see you pass elements of array in your function per call.
Maybe it's better to rewrite addMultiLink and consider it's argument as array? Or check if it is array or some integer value:
public function addMultiLink($argument)
{
if (is_array($argument)) {
// do a foreach loop for example
} else {
// do something else
}
}
$product_entry_field = (new EntryField('products'));
foreach($product_ids as $product_id) {
$product_entry_field->addMultiLink($product_id);
}
I don't know if I have seriously misunderstood concept of OOP or I am implementing it wrong here. Can please somebody explain what I am doing wrong here
<?php
class my {
public function myName(){
return get_class($this);
}
public function toArray() {
$retArray=array();
$class_vars = get_object_vars($this);
foreach ($class_vars as $name => $value) {
$retArray[$name]=$value;
}
return $retArray;
}
}
class you extends my {
private $yoo;
public function __construct($var1) {
$this->yoo=$var1;
}
}
$objectYou = new you("I am pri");
echo '<pre>';
echo $objectYou->myName();
echo '<br>';
print_r($objectYou->toArray());
echo '</pre>';
?>
Output is:
you
Array ( )
what I want here is to have super class my which has a function toArray to convert object to array. Now all the classes which extend this class should inherit this function and can use it. but here calling toArray on objectYou is returning empty array. At the same time calling myName on objectYou is showing name of child class.
The only property ($yoo) is private, and thus not accessible from my, where toArray() is defined.
Ok my problem is as follows;
I have a class that describes a pet with this constructor;
public function __construct($name, $type, $age)
So what I want to do is make a number of pet objects, then I want to print all the attributes of all the objects of this class so that it looks something like this
What is the best way of going about it? I know how to iterate through an object's variables, but my main concern is how to iterate through all objects of a certain class. I would love it if someone could show me a code example of something, particularly if there is a way to do it without the use of arrays.
Any help is appreciated!
You could, in the class constructor, append $this to a static array that keeps all the elements of this type:
class Pet {
public static $allPets = array();
function __construct($name, $type, $age) {
self::$allPets[] = $this;
// more construction
}
}
Your list of all Pet objects is now in Pet::$allPets.
Normally you would expect to have some way of tracking the instances you've created, maybe in an array or some kind of containing class.
But for the sake of argument, you could check all the variables in the current scope with get_defined_vars(), recursively searching any arrays or objects you find, with something like this:
function findInstancesOf($classname, $vars)
{
foreach($vars as $name=>$var)
{
if (is_a($var, classname))
{
//dump it here
echo "$name is a $classname<br>";
}
elseif(is_array($var))
{
//recursively search array
findInstancesOf($classname, $var);
}
elseif(is_object($var))
{
//recursively search object members
$members=get_object_var($var);
findInstancesOf($classname, $members);
}
}
}
$vars = get_defined_vars();
findInstancesOf('MyPetClass', $vars);
I guess it depends on your structure, but I´d have another object / class that contains all generated pet objects, so I would loop through that.
Well, you could make a custom create option and use static variables to store an instance of each created class
Class Pet
{
public static $pets = array();
public static create($name, $type, $age)
{
$pet = new Pet($name, $type, $age);
self::$pets[] = $pet;
return $pet;
}
}
Pet::createPet("test", "test", 42);
Pet::createPet("test", "test", 42);
Pet::createPet("test", "test", 42);
foreach(Pet::$pets as $pet)
{
echo $pet->name;
}
i would make a foreach loop
foreach($myobject as $key => $pent)
{
echo $key;
echo $pent;
}