Accessing object property returns property name with $ - php

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

Related

Fields of an Exception object when printed out

Please take a look at the output I get when I print out the contents of an Exception object (using the built-in class here):
Exception Object
(
[message:protected] => My custom message
[string:Exception:private] =>
[code:protected] => 2281488
[file:protected] => /home/user/scripts/test.php
[line:protected] => 18
[trace:Exception:private] => Array
(
)
[previous:Exception:private] =>
)
Most of the fields have the field_name:visibility_keyword format, but two of them have Exception appear in between. What does this denote?
Also, two other fields, namely $string and $trace, aren't mentioned in the official manual but I know the latter receives a value when an exception object is thrown within a function, so it contains the order in which functions are called until one of them creates said exception.
The string field gets assigned a value when I try to echo an exception and remains empty unless I echo it before the contents of the object are printed out. Can you please enlighten me on how this field works exactly?
I'd appreciate any input I can get. Thanks!
This has nothing to do with exceptions, it's just how var_dump() and print_r() display private and protected properties of objects.
Private properties are always shown with the name of the class before :private
That tells you which class in the inheritance hierarchy is allowed to access that property.
This isn't needed for protected properties, because any class in the hierarchy can access them.
class Class1 {
private $private1;
protected $protected1;
public $public1;
}
class Class2 extends Class1 {
private $private2;
protected $protected2;
public $public2;
}
print_r(new Class2);
displays
Class2 Object
(
[private2:Class2:private] =>
[protected2:protected] =>
[public2] =>
[private1:Class1:private] =>
[protected1:protected] =>
[public1] =>
)
Private properties aren't mentioned in the manual because they're not part of the public interface, they're just for internal use by the class. That's why they're declared private.
Protected properties are documented because you can access them if you define subclasses of Exception.
string:Exception:private.
string means a property of Exception class that has private visibility. Same with the previous and trace. These three properties are exclusively available only in the Exception class. As you noticed, protected properties have no class between because it can be passed on the child classes. This statement is also true to public properties.

PHP - type hinting for object, not the same as stdClass

Is it just me or is this behavior weird in PHP.
Lets say for instance that we have a function like this:
function test(object $arg)
{}
If I were to call this function:
test((object)'string');
object (the type hinting) would not refer to a stdClass and would result in an error even though object seems to be a reference to stdClass when looking at the built in settype() function in PHP. Even casting to an object would result in a stdClass but for some reason I can't use settype($foo, 'stdClass')...
Is there a reason behind this?
There is no overall object class definition in PHP. Even if you were to create a class called myClass and give an instance to the test function the code would not work. The typecast to object creates an instance of type stdClass.
You have to use function test(stdClass $arg) as the function definition.

How to read "protected" variable

i get a resultset from a class that manages WSDL Data.
I didn't write the code to the class, i only use it.
I call a function to create an ID with the service and want to work with that ID later in the same script.
My Resultset looks like this:
Array
(
[0] => SaveResult Object
(
[id:protected] => newgeneratedID
[success:protected] => 1
)
)
So I tried $response[0]->id to get the ID i need.
Now I get a fatal error.
PHP Fatal error: Cannot access protected property SaveResult::$id
I know it´s a noob question, but I don't get why I can print_r the object but not get the values inside.
You cannot use any of protected data from another space except the same object.
But you can edit the SaveResult object and add getter for id:
public function getId() {
return $this->id;
}
There ought to be a method you can call in the SaveResult class which lets you access the data, something like:
$response[0]->getId();
See the documentation/source code of the class.
You can read a protected property with the ReflectionProperty interface.
The HandyMan component from the phptoolcase library has a handy method to read inaccessible objects properties.
$value = PtcHandyMan::getProperty( $your_object , 'propertyName');
Static property from a singleton class:
$value = PtcHandyMan::getProperty( 'myCLassName' , 'propertyName');
Very simple and usefull, though it is only adviced in few situations as protected/private properties are shouldn't be used outside of their scope.
You can find the HandyMan class here: http://phptoolcase.com/guides/ptc-hm-guide.html

Get StdClass Object Property Name

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.

Get property from an object 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?

Categories