In PHP is it possible to change an Objects property key/name? For example:
stdClass Object
(
[cpus] => 2
[created_at] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
I wish to change the key created_at to created in the Object leaving an object that looks like:
stdClass Object
(
[cpus] => 2
[created] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
$object->created = $object->created_at;
unset($object->created_at);
Something like an adapter class may be a more robust choice though, depending on where and how often this operation is necessary.
class PC {
public $cpus;
public $created;
public $memory;
public function __construct($obj) {
$this->cpus = $obj->cpu;
$this->created = $obj->created_at;
$this->memory = $obj->memory;
}
}
$object = new PC($object);
No, since the key is a reference to the value, and not a value itself.
You're best off copying the original, then removing it.
$obj->created = $obj->created_at;
unset(obj->created_at);
Its similar to #deceze adapter, but without the need to create an extra class
$object = (object) array(
'cpus' => $obj->cpus,
'created' => $obj->created_at,
'memory' => $obj->memory
);
Related
Given that I'm getting different object results depending of the API I query, here are two examples out of 35 I have to implement:
stdClass Object
(
[mid] => 7127.75
[bid] => 7126.6
[ask] => 7128.9
[last_price] => 7128.8
[low] => 7000.0
[high] => 7492.1
[volume] => 53255.4195502
[timestamp] => 1510265647.9803913
)
stdClass Object
(
[Success] => 1
[Message] =>
[Data] => stdClass Object
(
[AskPrice] => 7095
[BidPrice] => 7070
[Low] => 7001
[High] => 7540
[Volume] => 17.38943459
[LastPrice] => 7090
[Change] => -1.02
[Open] => 7163
[Close] => 7090
)
[Error] =>
)
I want to build mapping variable array to access the object easily.
$xmap["a"]["bid"] = "bid";
$xmap["b"]["bid"] = "Data->BidPrice";
Let's assume $content has the first example object, this will work:
print $content->{$xmap["a"]["bid"]}; // the result is 7128.9
As for the second example object, it does not:
print $content->{$xmap["b"]["bid"]}; // the result is PHP Notice: Undefined property: stdClass::$Data->BidPrice in ...
Can this be done or am I stuck with if statements!
First, convert all the objects into assoc. arrays by using json_encode/decode. You'll find this code multiple times here in stackoverflow
$arr = json_decode(json_encode($obj), true);
Second, I recommend a dot-notation for the key-path, plus a tiny function to find the value in a multidimensional array.
Example:
function fromArray($key, $arr) {
$keys = explode('.', $key);
foreach($keys as $k) {
if (!isset($arr[$k]))
return array(); // return empty array
$arr = $arr[$k];
}
return $arr; // can be a scalar value or array
}
$key = 'my.super.array.content';
$array = ['my' => [
'super' => [
'array' =>[
'content'=>4711
]
]
]
];
var_dump($array, fromArray($key, $array));
/*
array(1) {
["my"]=>
array(1) {
["super"]=>
array(1) {
["array"]=>
array(1) {
["content"]=>
int(4711)
}
}
}
}
int(4711)
*/
I found the dot-notation very useful when dealing with complex structures.
You can convert your objects to arrays and build a large mapping array that needs to be maintained and then explode it and loop through it to access the other arrays; or you can try using patterns. I'm thinking Adapter, but maybe another is a better fit. This is using your second object as an example, but just add as many as needed:
class ContentAdapter {
public function __get($name) {
return $this->obj->{$xmap[$name]};
}
}
class ContentAdapter_API_B extends ContentAdapter {
public $xmap = ['bid' => 'BidPrice', 'ask' => 'AskPrice'];
public function __construct($obj) {
$this->obj = $obj->data;
}
}
Now it is consistant regardless of the object since each has an adapter:
$content = new ContentAdapter_API_B($content);
echo $content->bid;
Using your first object you can either create a child as well (ContentAdapter_API_A in case the structure ever changes) or instantiate directly:
$content = new ContentAdapter($content);
echo $content->bid;
Or obviously just use it as is:
echo $content->bid;
An alternate way without inheritance is to use getters:
class ContentAdapter_API_B {
public function __construct($obj) {
$this->obj = $obj->data;
}
public function getBid() { return $this->obj->BidPrice; }
}
So long as the methods are consistent then it will always work:
$content = new ContentAdapter_API_B($content);
echo $content->getBid;
My situation is quite simple, but i'm still looking for a nice and short solution for it.
Here is my case:
I receive a soap response object which is my be different from a call to another.
Sometimes, these properties are objects themselves and may have properties we have to get. For this, an array is set for each type of call to select the data wanted and discard the rest.
By example, in a call we receive an object like this:
(I made a code easy to test by mocking the received object)
$objTest = new stdClass();
$objTest->Content1 = "";
$objTest->Content2 = new stdClass();
$objTest->Content2->prop1=1;
$objTest->Content2->prop2=2;
$objTest->Content2->prop3=3;
$objTest->Content3 = 3;
$objTest->Content4 = array('itm1'=>1, 'itm2'=>'two');
i want to check if $objTest->Content2->prop3 exist, but i don't know at the right moment i am looking for this because what i'm looking for is in the associative array.
The array for the call look like:
$map = array('Content3','Content2->prop3');
From now i am able to get the content of the Content3 property by doing this:
foreach ($map as $name => $value) {
if (isset($object->$name)) {
echo "$value: ". json_encode($object->$name)."\n";
}
}
But not for the other because of the reference "->".
Now my question:
Is there a way to get an unknown property of an unknown object as displayed above?
This is a result of the previous test:
Dump of objTests:
object(stdClass)[1]
public 'Content1' => string '' (length=0)
public 'Content2' => object(stdClass)[2]
public 'prop1' => int 1
public 'prop2' => int 2
public 'prop3' => int 3
public 'Content3' => int 3
public 'Content4' => array (size=2)
'itm1' => int 1
'itm2' => string 'two' (length=3)
Trying to access the proprerty prop3 of the content2 of the object with a string:
Standard way to get the value : $objTest->Content2->prop3
Result : 3
Test string: "Content3"
Result: 3
Test astring: "Content2->prop3"
( ! ) Notice: Undefined property: stdClass::$Content2->prop3
Hope i put everything to help understand my situation!
Thanks!
I don't know of a built-in PHP function that does this, but a function could be used to break up the string of properties and iterate through them to find the value of the last one in the string.
function get_property($object, $prop_string, $delimiter = '->') {
$prop_array = explode($delimiter, $prop_string);
foreach ($prop_array as $property) {
if (isset($object->{$property}))
$object = $object->{$property};
else
return;
}
return $object;
}
how would I go about getting around the "protected" so I can output the data.
tabs\api\property\Property Object (
[id:protected] => 90_4_HH
[propertyRef:protected] => 90_4
[brandCode:protected] => HH
[url:protected] => http://hh.api.carltonsoftware.co.uk/property/90_4_HH
[accountingBrand:protected] => HH
[slug:protected] => 90-4-hh
[name:protected] => Carreg Lwyd Farmhouse
[address:protected] => tabs\api\core\Address Object (
[addr1:protected] => Port Eynon
[addr2:protected] =>
[town:protected] => Gower
[county:protected] => Swansea
[postcode:protected] => SA3 1NN
[country:protected] => GB
)
[changeOverDay:protected] => Saturday
[calendar:protected] => http://hh.api.carltonsoftware.co.uk/property/90_4_HH/calendar
[booking:protected] => http://hh.api.carltonsoftware.co.uk/booking
[pets:protected] =>
[promote:protected] =>
[smoking:protected] =>
[shortlist:protected] =>
[accommodates:protected] => 12
[rating:protected] => 5
[bedrooms:protected] => 6
[images:protected] => Array (
[90_4p1190276.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372] => tabs\api\property\Image Object (
[filename:protected] => 90_4p1190276.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372
[title:protected] => Carreg Lwyd Farmhouse, Port Eynon
[alt:protected] => The Lounge
[url:protected] => http://hh.api.carltonsoftware.co.uk/image/normal/1000x750/90_4p1190276.jpg
[height:protected] => 750
[width:protected] => 1000
[apiPath:protected] => http://hh.api.carltonsoftware.co.uk
)
[90_4img_4819.jpg?APIKEY=homefromhome&hash=31b3f7b1b377184e8cb8fb64d434a11a4c3446c1091535ef6db4e119689a6372] => tabs\api\property\Image Object (
The dump:
foreach ($properties as $property) {
echo sprintf('<p class="listit">%s</p>', $property);
print_r($property);
}
I did not fully understand your question, but if you want to access protected properties from outside the class, you have to use Reflection:
$reflObj = new ReflectionObject($property);
$props = $reflObj->getProperties(ReflectionProperty::IS_PROTECTED);
foreach ($props as $prop) {
$prop->setAccessible(true);
echo $prop->getName() . ":" . $prop->getValue($property), "\n";
}
Sample for outputting the address:
$reflObj = new ReflectionObject($property);
$addrProp = $reflObj->getProperty('address');
$addrProp->setAccessible(true);
echo $addrProp->getValue($property);
Your title implies that you want to make -the class (not object)- public, as opposed to "internal"/etc.. All classes are public in PHP.
Your question says that you want to get around method/property scope (disagrees with title). You didn't say what you've tried. You also didn't indicate whether this is a class you developed or not. If you have control over it, add a freaking method or two to allow you to get the data. If you don't, then inspect the methods, and/or attempt reflection. These answers apply to every OOP language in existence.
Before asking other people, read the documentation.
http://www.php.net/manual/en/language.oop5.basic.php
Answer to the question posed by the title is that all classes are public.
What you are asking is how to access protected member variables.
Taken from here (http://ajmm.org/2011/06/using-php-reflection-to-read-a-protected-property/), this is an example of how to do this:
public static function getReflectedPropertyValue($class, $propertyName)
{
$reflectedClass = new ReflectionClass($class);
$property = $reflectedClass->getProperty($propertyName);
$property->setAccessible(true);
return $property->getValue($class);
}
...
getReflectedPropertyValue($yourObject, 'protectedProperty');
That said, the question is why you want to do this. Members are marked protected specifically to prevent you from doing this. If you have access to the source code that defines this other class, then it might make more sense to either change these members to "public" or (better) to provide a getXYZ() method for whichever properties you want to access.
I want to convert array of objects to json encoding, I make like this
$allVisits = $mapper->getAllVisits($year, $month);
echo json_encode($allVisits);
and here's is getAllVisists method
function getAllVisits($year, $month) {
$where = array(
'year = ?' => $year,
'month = ?' => $month
);
$resultSet = $this->getDbTable()->fetchAll( $where);
$visitsEntries = array();
foreach ($resultSet as $row) {
$entry = new Visits_Model_Visit();
$entry->setId($row->visit_id)
->setDay($row->day)
->setDate($row->date)
->setTarget($row->target)
->setStatus($row->visit_status)
->setTime($row->visit_time);
$visitsEntries[] = $entry;
}
return $visitsEntries;
}
when I echo the size of $allVisits it return correct number of records, but in js the values are received empty like this [{},{},{},{}]
Edit
When I print_r($allVisists) brfore encoding it it returns
Array
(
[0] => Visits_Model_Visit Object
(
[day:private] => sunday
[date:private] => 2012-03-06
[target:private] => شسي
[id:private] => 1
[status:private] => 0
[time:private] => 12:00:00
)
[1] => Visits_Model_Visit Object
(
[day:private] => sunday
[date:private] => 2012-03-06
[target:private] => clinnics
[id:private] => 4
[status:private] => 0
[time:private] => 00:00:00
)
[2] => Visits_Model_Visit Object
(
[day:private] => Tuesday
[date:private] => 2012-03-06
[target:private] => clinnics
[id:private] => 5
[status:private] => 0
[time:private] => 00:00:00
)
[3] => Visits_Model_Visit Object
(
[day:private] => Wednesday
[date:private] => 2012-03-28
[target:private] => ??????? ???????
[id:private] => 7
[status:private] => 0
[time:private] => 12:00:00
)
)
You are using json_encode with objects that don't have any public members. json_encode only works on the members it can "see", that's why those are empty.
Since PHP 5.4 you can make use of the JsonSerializable interface to control which data will be offered for json_encode, e.g.:
class Visits_Model_Visit implements JsonSerializable {
...
public function jsonSerialize() {
return (object) get_object_vars($this);
}
...
}
If you are below 5.4 you can also implement that function w/o extending from the interface and then assigning the correct value manually:
$visitsEntries[] = $entry->jsonSerialize();
Hope this helps.
As Ray says if your class properties are protected or private, these will not be jsoned.
Since PHP 5.4 instead of using the commented toJson method, you have the ability to specify which data will be serialized implementing the JsonSerializable interface, so json_encode knows how to work on this.
/* PHP >= 5.4 only */
class Visits_Model_Visit implement JsonSerializable {
public function jsonSerialize()
{
return array(
'day' => $this->day,
'date' => $this->date,
'target' => $this->target,
'id' => $this->id,
'status' => $this->status,
);
}
}
By default, json_encode() only serializes public properties of an object. Making all properties you want serialized public is NOT the solution! PHP 5.4 and later has the JsonSerializable interface, but I propose a straightforward solution for earlier versions of PHP.
Since JsonSerializable is only part of PHP 5.4 and later, create it yourself.
if (!interface_exists('JsonSerializable')) {
interface JsonSerializable {
public function jsonSerialize();
}
}
That wasn't so hard, was it? Now we can implement JsonSerializable without worrying about what version of PHP we are using!
class Visits_Model_Visit implements JsonSerializable {
...
// Only put properties here that you want serialized.
public function jsonSerialize() {
return Array(
'day' => $this->day,
'date' => $this->date,
'target' => $this->target,
'id' => $this->id,
'status' => $this->status,
'obj' => $this->obj->jsonSerialize(), // example for other objects
'time' => $this->time
);
}
...
}
Now you can just call jsonSerialize() to get an associative array that you can encode with json_encode().
...
$entry = new Visits_Model_Visit();
$entry->setId($row->visit_id)
->setDay($row->day)
->setDate($row->date)
->setTarget($row->target)
->setStatus($row->visit_status)
->setTime($row->visit_time);
$visitsEntries[] = $entry->jsonSerialize();
...
You may then call json_encode($visitsEntries) to get your desired result.
[
{
"day":"sunday",
"date":"2012-03-06",
"target":"\u0634\u0633\u064a",
"id":1,
"status":0,
"time":"12:00:00"
},
{
"day":"sunday",
"date":"2012-03-06",
"target":"clinnics",
"id":4,
"status":0,
"time":"00:00:00"
},
...
]
Are the properties private or protected for the object? If so, json encode can't see them inside the object. I get around this by creating a 'toJson' method in my objects that I need to serialize into json. In this method, I walk the objects properties and manually construct a generic object, that I pass to json_encode. Then I return the Json string from this method.
Do not just make all your object properties public!!!!
For those who are looking for simple answer, unlike other complicated answers my is piece of art:
json_encode(array(
Protocol::PARAM_CODE => Protocol::CODE_SUCCESS,
Protocol::PARAM_USER => (object)$user->jsonSerialize()
));
Even when $user->jsonSerialize() outputs stdObject, json_encode is so dumb, it has no idea it is object so you have to state that explicitly with casting it to (object) - don't you love PHP for it's simplicity?
I'm relatively new to OOP in PHP, and I'm not sure if what I'm trying to do is possible or recommended. In any case, I can't figure it out. I'd appreciate any pointers to tutorials or documents which might help - I'm not expecting a full-blown answer here.
I have a system in which each user has a number of 'Libraries'. Each Library contains a number of 'Elements'.
DB set up is as follows:
user_libraries
- id (unique)
- user_id (identifies user)
- name (just a string)
elements
- id (unique)
- content (a string)
library_elements
- id (unique)
- library_id
- element_id
where library_id is the id from user_libraries, and element_id is that from elements.
I want to be able to access a given user's library, and their elements.
I've set up the library class, and can use it to retrieve the list of libraries (or a sub-list).
I do this like this:
$mylibraryset = new LibrarySet();
$mylibraryset->getMyLibraries();
which gives (when I use print_r):
LibrarySetObject (
[user_id] => 105
[data_array] => Array (
[0] => Array (
[id] => 1
[user_id] => 105
[type] => 1
[name] => My Text Library
)
[1] => Array (
[id] => 2
[user_id] => 105
[type] => 2
[name] => Quotes
)
)
)
Now, what I'd like to be able to do is for each of those libraries (the elements in data_array), to retrieve all the elements.
The best idea I've had so far is to do something like:
foreach($mylibrary->data_array as $library) {
$sublibrary = new Library();
$sublibrary -> getAllElements();
}
where Sublibrary is another class which has the function getAllElements. I can't quite get it to work though, and I'm not sure I'm on the right lines...
Is there a way that I can then end up being able to do something like this:
$mylibrary->sublibraries[0]->element[0]
to retrieve a specific element?
As I say, I don't expect a full-blown explanation here - just pointers to get me started.
<?php
class Library {
public $element;
public $data;
public function __construct($sublibrary) {
$this->data = $sublibrary;
}
public function getAllElements() {
// populate $this->element using $this->data
}
}
class LibrarySet {
public $user_id;
public $data_array;
public $sublibraries;
public function getMyLibraries() {
// populate $this->data_array
$this->sublibraries = Array();
foreach($this->data_array as $index => $sublibrary) {
$this->sublibraries[$index] = new Library($sublibrary);
$this->sublibraries[$index]->getAllElements();
}
}
}
$mylibraryset = new LibrarySet();
$mylibraryset->getMyLibraries();
$mylibraryset->sublibraries[0]->element[0]
?>