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?
Related
I am trying to write a scope and I need to know if any of the previous scopes added any ->select() information earlier and if not, then add it (which would erase the previous stuff).
The scope gets a $query object which is \Illuminate\Database\Query\Builder (according to the docs anyway)
If I look into Builder.php I see the $columns property which is used to store the columns to fetch
/**
* The columns that should be returned.
*
* #var array
*/
public $columns;
However in my scope if I try to access it using
$query->columns
I get an error message saying:
PHP error: Undefined property: Illuminate\Database\Eloquent\Builder::$columns
As you can see there is some sort of magic going on behind and it is looking at Eloquent\Builder and not at Database\Query\Builder.
I am probably missing something simple here, maybe need more coffee again... Any pointers are welcome
Thanks
PS. I know I can use addSelect() but I would like to know why I am not able to access the 'columns' variable still...
The object that gets passed to your scope method is of class Illuminate\Database\Eloquent\Builder, while the class that has the columns attribute is Illuminate\Database\Query\Builder. Object of the first class uses internally object of the second class so if you want to access its public properties you need to do:
public function scopeFunction($query) {
$columns = $query->getQuery()->columns;
}
Keep in mind that some scopes can be applied after your scope is called and modify $columns attribute after your scope is run.
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
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.
In PHP, you can normally place an object in an array, like so:
class Car{}
$car = new Car();
// This runs without error
$array['vehicle'] = $car;
I have a custom MVC framework I've built, and I need the controller to get an ORM object from the model, so it can pass that to the view. So, I initialize my user object:
$user = new User(2);
Now, I want to put that user object into a $data array so it can be passed to the view:
($user->data returns an ORM object)
$array['user'] = $user->data;
The problem is, after doing this, I receive the following error:
Object of class ORM could not be converted to string
What am I doing wrong? Is there something I'm missing?
Thanks for any help in advance.
Edit: Here's what $user->data refers to, this is from the constructor of class User:
$this->data = ORM::for_table("users")->find_one($this->user_id);
(I'm using Idiorm as an ORM)
If you get an error message like:
Object of class ORM could not be converted to string
The first question you should ask is, "why does it have to be converted to a string"? An array can take a string just fine, so you can guess that $data is actually a string and PHP thinks you want to modify $data[0].
As you've seen, dynamically typed languages can leave befuddled if you aren't careful.
When your variables show suspect behavior, try to see what's actually in them using var_dump().
It's also a good idea to explicitly initialize arrays (eg: $my_array = array();) before using them.
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.