I have the following objects which I pass to a function:
$this->cars->car1->engine='v8';
$this->cars->car1->hp='330';
$this->cars->car2->engine='v6'
$this->cars->car1->hp='210';
handle_car($this->cars->car1);
handle_car(this->cars->car2);
The function handle_car needs to find out what car it is ie whether its "car1" or "car2".
How can I go through the property keys to see if the property is car1 or car2? I have tried using the get_object_vars function but it only returns the keys of 'car1' and 'car2' not those itself.
You are passing property values to your function, not property names. You should pass name directly to determine it inside function properly. Remember, $this->cars->car1 or $this->cars->car2 are just objects, where could be anything.
Related
I am having trouble accessing the object property. It should be straightforward, but I am getting
Notice : Undefined property: myClass::$myname:protected in ...
My object, when I do a print_r looks like this:
myClass Object
(
[myname:protected] => Array
(
[key1] => stdClass Object
(
[slug] => name
[name] => Big Name
)
.
.
.
[mykey] => stdClass Object
(...
And I tried printing
print_r($this->{'myname:protected'});
to see if I'll get the array out, from which I can get what I need. But I got the above error.
print_r($this); returns my object, since I am inside a public function (method) in my class myClass.
Why can't I access that key, but when I do print_r($this->mykey->property1) for instance, I can easily get any property for mykey object?
Why is it adding $ in front of the name? I know some oo php, but I'm still a beginner. I have a
protected $myname;
at the beginning of the file, and I'm using it in my __construct() like
public function __construct($myname){...}
Note: I am trying to modify and figure out someone else's code, so I'm not 100% sure what every single piece does yet :\
EDIT:
When I do
print_r($this->myname);
I get the array I need. Can anyone clarify why is that? Thanks.
The member name is myname - the :protected shown in the output of print_r is just debugging information to show that its a protected member of the object.
So from a class method, you would access the member as:
$this->myname
I'm trying to pull a random instance of a class (object) by generating a random number and checking object ids against that number.
I've found a lot of info on how to retrieve an object attribute (specifically here it's the id) but not without knowing which object first.
So within my class I've got
public function getID() {
return $this->id;
}
But
getID()
only works if I use it as
$object->getID()
Is there a way to do something similar but for every object in a class, without specifying the objects?
I'm trying to avoid having to write if/then for every object in the class.
You could set up an array of objects, then iterate over the array and call the getID() method on each object. If your array of objects is called $myObjects...
foreach($myObjects as $object) {
$object->getID(); //And do something with it
}
However, if you want to pick a random object out of a set of objects, testing a whole bunch of them to see if they are the object you picked isn't really ideal. You'd be better off putting them into an array and using array_rand() to select a random object out of the array.
What's your purpose for doing this? That may indicate a better way to approach this.
I think you'd have to have planned for this eventuality, then loop thru the candidate objects as #Surreal Dreams; suggests.
See Get all instances of a class in PHP
Have to do some php coding but I am totally new to it. The question is I was trying to get a property value from an object called $mgm_member, this object is from wordpress plugin which records website's member info. First I use:
var_dump(get_object_vars($mgm_member));
The results shows that this obeject has 37 properties and all of them have values. Than I tried use
var_dump($mgm_member->phone);
The result is null. I tried many other properties but only the first property of this object have value. Can anyone help me? Thank you very much.
well I suppose if the scope of "phone" is private var_dump will not be able to access and view it, is it? In my case, I can view all properties and their values using var_dump() function but when I tried to get one property it just doesn't work. However,I can get the first property "id" using the same code
echo $mgm_member->id;
That is really weird.
As the property is 'private' you will need to make a function to access and return it.
A dump may display them but you WILL NOT be able to directly access a 'private' property.
class .... {
public function getPhone()
{
return ($this->phone);
}
}
then:
echo $mgm_member->getPhone();
var_dump(get_object_vars($mgm_member)); shows the object variables. If you are getting data with this line of code, you have data in your object.
You can access properties of a variable in your code with $mgm_member->phone - why not do so?
If you want to place the data into a variable you can use something like this:
$myVar=$mgm_member->phone;
but that defeats the purpose of OOP. Why not refer to it as exactly $mgm_members->phone all the way through your code?
Edit: As you point out, the property is private which means that only the object itself can use it. You could get around this by modifying the object, but this may be a nasty approach - it is private for a reason. Can you not use functions within the object to display the values? Alternately, is there a function you can use in the object to return you a clone of the object with different property attributes?
So by implementing Iterator, ArrayAccess, and Countable built-in interfaces, we have control over what happens inside an object when it's used in foreach loops or if a property is accessed as if it were an array index ($object['id']).
For example, if you wanted, you could set it up so $object['version'] += 1 could automatically increment version field in the database.
What's missing is casting the object to array. Is there any interface or class that allows control over what happens when you do: (array) $object? Any built-in interface or class at all, no matter how obscure? For example: if I wanted (array) $object to return $this->propertyArray instead of the normal object to array conversion of dumping all public object properties?
Note: something like requiring calling $object->toArray() by method name doesn't count, as the idea is to minimize the outside differences between an array and object as much as possible.
no there is not , because toArray() is not an magic function like __toString(); where casting works e.g
$foo = (string) $myObect;
you have to specify toArray() and inside it return your array , may be in future __toArray() might come.
You could add a method like this
public function toArray() {
return get_object_vars( $this );
}
See here. Or check SplFixedArray::toArray.
Please stop me if i am doing something wrong. It works but somehow it doesn't appear the right way to me... Look at the member function call in talks.php. Does this look right to you? Is there a better way to solve that? Thanks.
show.php
I am passing my user class by reference:
$talks = new talks($comments, $user);
talks.php:
[...]
function __construct($comments, &$user)
{
//Passing user class
$this->user = $user;
[...]
if ($this->user->is_loaded()){}
This looks a-ok to me. What problem do you see with it?
In php 5, objects are always passed by reference.
From http://www.php.net/manual/en/language.oop5.references.php:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
So, you should not need the "&" operator in the parameter list of your constructor.