I have an object that has a property link. I get it with getLink(). This property is actually an array so I get the 1st element $link[0] which in turn is an object so I get the property getHref(). My total code looks like this.
$link = $obj->getLink();
$link0 = $link[0];
$href = $link0->getHref();
Is there a better way to write this code, like in 1 line? something like $obj->getLink()->get(0)->getHref() I'm using PHP 5.3.4
current($obj->getLink())->getHref()
That's not very elegant though.
Your code should probably look like this:
$links = $obj->getLink();
$href = $links[0]->getHref();
If getLink always returns an array it should be called getLinks (plural) and I would expect to have to iterate over the return value; working only with the first result seems weird. Alternatively, getLink could accept a parameter to return a specific link, e.g.:
$href = $obj->getLink(0)->getHref();
PHP doesn't support direct return value dereferencing, so either the object interface is not very well suited for the language it's written in or you're not using the object as expected.
Related
I want to convert two different PHP tags into a single PHP tag.This may sound a little weird but recently I found a question like this and answer with correct marked.
I can't find I'm currently lost address.
My question:
for example;
$gates = array('t1','t2','t3',t4','t5');
$openGates-> and $gates merge.
Result:
$openGates->t1; or t2,t3.
If I remember correctly, the answer to the problem I found before was;
$openGates->{$gates}; like this. I not sure
How can I do that?
You seem to be confusing objects with arrays. Arrays simply contain data, unless you create them with keys also. Such as:
$gates = array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
Matthew Page is correct in that you should look up PHP OOP for a solution.
That being said, you can cast your array to an object providing it has both keys and values:
$gates = (object) array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
or
$openGates = (object) $gates;
This will allow you to access 'properties' of the object in the way you have demonstrated:
echo $openGates->t1;, for example. The -> operator only applies to objects, which are instances of classes, not arrays.
If you do cast your array to the type of object, be sure that you have both keys and values.
It's not simple for newbie programmer...
At first:
$gates = array('t1','t2','t3','t4','t5');
It's Array
$openGates->
This is class instance. Btw. You can retrieve class instance var like $className->varName
You can't simple merge array and class instance. But you can create new class instance variables by loop.
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gatesVal = NULL;
}
But I think it's, in result should be like that:
$gates = array('t1'=>'opened','t2'=>'closed','t3'=>'closed','t4'=>'opened','t5'=>'opened');
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gateKey = $gateVal;
}
echo $openGates->t1;
// or
foreach($gates as $key=>$val) {
echo $openGates->$key.PHP_EOL;
}
Btw you can simple set $openGates->gates = $gates; and call it like echo $openGates->gates['t1'];
I want to get the number of values that were submitted via my $_GET. I'm going to be using a for loop and every 7 are going to be saved to a class. Does anyone know how to find the Size_of the $_GET array? I looked through this info $_GET Manual, but don't see what I'm looking for.
This is a code snippet that I started in the class that I go to after the submit.
for($i=0;$i<$_GET<size>;$i++) {
$ID = $_GET["ID"][$i];
$Desc = $_GET["Desc"][$i];
$Yevaluation = $_GET["Yevaluation"][$i];
$Mevaluation = $_GET["Mevaluation"][$i];
$Cevaluation = $_GET["Cevaluation"][$i];
$Kevaluation = $_GET["Kevaluation"][$i];
$comment = $_GET["comment"][$i];
//saving bunch to class next
}
$_GET is an array. Like any other array in PHP, to find the number of elements in an array you can use count() or its alias sizeof().
count($_GET["ID"])
could work. However, then you have to be sure that each index contains the same number of elements.
More secure would be
min(count($_GET["ID"]), count($_GET["Desc"]), ...)
With array_map this can probably be shortcut to something like:
min(array_map(count, $_GET))
Try thinking through the last one, if it seems difficult to you. It's called functional programming and it's an awesome way to shortcut some stuff. In fact array_map calls another function on each element of an array. It's actually the same as:
$filteredGet = array();
foreach ($element in $_GET) { // this is the array_map part
$filteredGet[] = count($element);
}
min($filteredGet);
Even better would be a structure where $i is the first level of the array, but I think this is not possible (built-in) with PHPs $_GET-parsing.
Example objects:
$this->obj1->lvl1
$this->obj1->lvl1->lvl2
I know I can access objects like this:
$var = 'obj1';
$this->{$var}
But I want to go further. The problem is that it needs to be dynamic so the name needs to come from a string. I'm using this for mapping. So a user can use dot notations to access anything in the object. So if the user uses this notation:
'obj1.lvl1'
'obj.lvl1.lvl2'
So all I have to do is:
$this->obj1->{$mapped_string}
So $mapped_string can go either one level or two or more levels deep.
It will map directly to the object. Anyone know how I can accomplish this?
Split the string into accessors, and then drill down in a loop. This works for any length of accessors:
$obj = $this;
$accessors = explode('.', $mapped_string);
foreach ($accessors as $acc) {
$obj = $obj->{$acc};
}
var_dump($obj);
Say we have the object $teams (an associative array) containing an object that provides the method getMembers() which returns an Array as follows:
$membersArray = $teams['blueteam']->getMembers();
If I want to then access individual members, I may do so as follows:
$membersArray[1];
Why can't I perform the access in-line as follows, and is there a proper way to do so in PHP?
$membersArray = $teams['blueteam']->getMembers()[1];
Support for this added in PHP 5.4.0:
$membersArray = $teams['blueteam']->getMembers()[1];
Rather than try to access it like that, why not make an alternative method called getMember() which accepts a parameter for the array index. For example:
function getMember( $index )
{
return $this->members[$index];
}
This makes your code a little more self-documenting by indicating getMembers will return an array of members, where getMember() will only return a single array element.
I'm pulling records from database, and I have a filed called content_fr
the _fr is being dynamicaly created based on a site language.
Getting all records from the table gives me of course content_fr field. What I need to do is manually assign the suffix in this case _fr if I try to concatenate $row->content.'_fr' I get the error Notice: Undefined property: stdClass::$content, which makes sense since $content does not exist, but $content_fr does. Using standard arrays I was able to do it like $row['content_fr'], and worked fine, but now using stdClass I'm getting an error.
How can I convert $row->content.'_fr' into one string, so that php sees it as $row->content_fr ?
Try it like this :
$row -> {'content_' . $language}; // assuming $language = 'fr'
You can do it as follows:
$row->{'content_fr'};
EDIT: By the way, this question has been asked here many times. See previous threads such as this one: Accessing object's properties like if the objects were arrays.
Try something like this:
$lang = 'fr';
$property = 'content_' . $lang; // content_fr
$row->$property = 'something';