so I have this JSON object, which I have converted into a PHP object, for example I can use $apiobject->Response->DataItems and get a response.
This is stored in a class called returnjsonObject with a public function called getJsonObject.
In the same file how can I populate another class using the data from this $apiobject into something like:
class Response
{
public $StatusCode;
}
Then how can I for example echo out $StatusCode
Here is part of my file:
class Response
{
public $StatusCode; //*I want this to equal $apiobject->Response->DataItems*
}
class returnjsonObject{
public function getJsonObject()
{
echo"<pre>";
$apiobject = json_decode($response);
var_dump($apiobject->Response->DataItems);
echo"<pre>";
}
I've heard of using $this but I have to admit I don't understand it.
tl;dr I need to use $apiobject to populate $StatusCode using $apiobject->Response->DataItems
Hope you understand my question :-)
You can use setter and getter
class Response{
public $StatusCode;
/**
......
*/
public function __construct($attributes = Array()){
// Apply provided attribute values
foreach($attributes as $field=>$value){
$this->$field = $value;
}
}
function __set($name,$value){
if(method_exists($this, $name)){
$this->$name($value);
}
else{
// Getter/Setter not defined so set as property of object
$this->$name = $value;
}
}
function __get($name){
if(method_exists($this, $name)){
return $this->$name();
}
elseif(property_exists($this,$name)){
return $this->$name;
}
return null;
}
}
$DataItems =$apiobject->DataItems;
$response = new Response($DataItems);
echo $response->StatusCode;
Code not tested though. Learn more here.
http://www.beaconfire-red.com/epic-stuff/better-getters-and-setters-php
I would first set all attributes in Response Class from $apiobject like this
<?php
class Response {
public $StatusCode;
/* more attributes
...
*/
public $Name
} //end of Response class
Then instantiate Response class and set attributes
$response = new Response();
$response->StatusCode =$apiobject->DataItems->StatusCode;
$response->Name=$apiobject->DataItems->Name;
/* Set all other properties
.......
*/
echo $respose->StatusCode; //output status code
Note: you use $this to make reference to a function or a property in the current class. i.e
Class Example {
public $name;
public function getThatName(){
return $this->name; //we make reference to property `$name`
}
}
$this can only be use in a Class or Object to reference a property or a function in itself.
You can not do this.
$example = new Example();
$this->name; //Wrong because you are not in `Example` class
Related
I have a php class like,
<?php
namespace app\test;
class TestObject
{
private $obj_id;
private $obj_name;
public function __construct($obj_id, $obj_name)
{
$this->obj_id = $obj_id;
$this->obj_name = $obj_name;
}
public function get_obj_id()
{
return $this->obj_id;
}
public function get_obj_name()
{
return $this->obj_name;
}
}
And in my index.php I am trying to create a TestObject and json encode it with,
$obj_1 = new TestObject(1, "testname");
echo json_encode($obj_1);
But as output I get only {}
Any idea, why it is not showing object fields?
"why it is not showing object fields"
...because they are marked private. As soon as they are marked public they will be visible to json_encode().
class TestObject
{
public $obj_id;
public $obj_name;
public function __construct($obj_id, $obj_name)
{
$this->obj_id = $obj_id;
$this->obj_name = $obj_name;
}
public function get_obj_id()
{
return $this->obj_id;
}
public function get_obj_name()
{
return $this->obj_name;
}
}
$obj_1 = new TestObject(1, "testname");
echo json_encode($obj_1);
Output:
{"obj_id":1,"obj_name":"testname"}
Demo: http://sandbox.onlinephpfunctions.com/code/528d3b495cd23c9ffbea424556cd042c486c413c
Alternatively if you need to keep the properties private and still JSON-encode them, there are various techniques you can employ - see PHP json_encode class private members for details.
I can not load data to properties using this construction I receive null in dump
<?php
namespace App\Domain\Good;
class GoodDto
{
public $name;
public $articul;
public $price;
public $type;
public $qnt;
public $discount;
public $category;
public $description;
public $description2;
public $color;
public function load($data)
{
$this->name = $data['name'];
$this->articul = $data['artikul'];
$this->price = $data['price'];
$this->type = (isset($data['type'])) ? $data['type'] : null;
$this->qnt = $data['count'];
$this->discount = $data['spinner-decimal'];
$this->category = $data['id_cat'];
$this->description = $data['editor1'];
$this->description2 = '';
$this->color = $data['color'];
//$this->user_id = Auth::user()->id;
}
public static function fromRequest($request)
{
dump('inp=>',(new self ())->load($request->input()));
return (new self ())->load($request->input());
}
}
Please explain to me why I receive null while request->input() is an array, I call it from another place
$dto=GoodDto::fromRequest($request);
Method chaining, returns the last return from the chain. The other returns are used to call the next link in the chain.
(new self ())->load()
So load() needs to return $this
public function load($data)
{
...
return $this;
}
Currently it returns null, which is why it returns null.
See you are not saving the instance from the constructor, instead you pass it to load by enclosing it within the (....). By pass it I mean you call the load method on the return from the constructor.
You can test this like so:
class foo{
function load(){
return $this;//return this
}
}
var_dump((new foo)->load());
class bar{
function load(){
//return null
}
}
var_dump((new bar)->load());
Output
//return this
object(foo)#1 (0) {
}
//return null
NULL
sandbox
The second class in the example above class bar, is essentially what you are doing.
PS. forgot to scroll down on your post at first ... lol ... So I had to update my answer.
Bonus
You can also simplify the load code like this:
public function load($data)
{
foreach($data as $prop=>$value){
if(property_exists($this,$prop)) $this->$prop = $value;
}
return $this;
}
This way if you add new properties you don't have to edit the load method ever again, you just have to name the array elements the same as the class properties. You can even throw an error if the property does not exist if you want, by adding an else to the condition etc...
Personally, when I do this I prefer to call a set method like this:
//eg. $data = ['foo' => '2019-06-16']
public function load(array $data)
{
foreach($data as $prop=>$value){
$method = 'set'.$prop; //$method = 'setfoo' using the example above
if(method_exists($this,$method )){
$this->$method($value); //calls 'setfoo' with '2019-06-16'
}else{
throw new Exception('Unknown method '.$method);
}
}
return $this;
}
public function setFoo($date){
$this->foo = new DateTime($date);
}
Then you can apply some transforms to the data etc... PHP method names are not case sensitive. You can even combine these by first checking for a method then a property then throw the error etc...
Cheers.
I am trying to understand how to efficiently create a new class object and set the variables directly.
I have a class:
class element_model
{
public $sType;
public $properties;
}
I have a controller in which the following function is defined:
public function create_element($sType, $properties)
{
$oElement_model = new element_model($sType, $properties);
return new element_model($sType, $properties);
}
But this does not returns a new element_model with properties set, it just returns an empty object.
It does not, however, throw an error.
What is the reason the function above does not work?
You have to pass to the constructor of the class, in PHP you should have a method in the class __construct :
class element_model
{
public $sType;
public $properties;
public function __construct($type, $property)
{
$this->sType = $type;
$this->properties = $property;
}
}
Then you can access them (note the variables are public)
$elem = new element_model($sType, $properties);
$elem->sType;
Although in some cases it is better to encapsulate vars (declare them private):
class element_model
{
private $sType;
private $properties;
public function __construct($type, $property)
{
$this->sType = $type;
$this->properties = $property;
}
public function getType()
{
return $this->sType;
}
public function getProperty()
{
return $this->properties;
}
}
Then you can access the variable through a getter
$elem = new element_model($sType, $properties);
$elem->getType(); //and
$elem->getProperty();
You must create a __construct function in your class that accepts the parameters and sets your variables. Like this:
class element_model{
.
.
.
public function __construct($type,$properties)
{
$this->sType = $type;
$this->properties = $properties;
}
}
The __construct function will be called when you create the object.
But if you want to be extra cool in programming, just define your properties as private and create getter and setter functions to access the variables of your object
private $sType;
public function getSType(){
return $this->sType;
}
public function setSType($value){
$this->sType = $value;
}
Hi please have a look on bellow code.
<?php
class A
{
public $name;
public function getName()
{
return $this->name;
}
}
class B extends A
{
public function setName()
{
$this->name = 'Prasad';
}
}
$obj = new B();
echo $obj->getName();
?>
Here It's display nothing when I echo the name. Related to the $name in class A. Is this issue is with getName or setName? How can I set the $name variable in class A from extended class B. And how can I get that from a class B object. Appreciate any hint or explanation on what I have missed.
You didn't set the name (using $obj->setName()) before.
Technically, it's echoing the $name variable (which is undefined at that point). Unfortunately, it hasn't been set yet. Try using $obj->setName() to set the name.
You are most of the way there with your code, in fact you would have a working example if you added a line telling the code to call the setName() function:
$obj = new B();
$obj->setName();
echo $obj->getName();
More typically you would use a set function with a parameter, then pass the value you want to set. You would also set the $name property to protected, which means the value must be accessed via the set & get methods (more on visibility in the manual):
<?php
class A
{
protected $name;
public function getName()
{
return $this->name;
}
}
class B extends A
{
public function setName($name)
{
$this->name = $name;
}
}
$obj = new B();
$obj->setName('Prasad');
echo $obj->getName();
?>
Yes, as SomeKittens suggested you need to call setName() first.
class A
{
public $name;
public function getName()
{
return $this->name;
}
}
class B extends A
{
public function setName()
{
$this->name = 'Prasad';
}
}
$obj = new B();
$obj->setName();
echo $obj->getName();
However, it might be better to perform the setting of the name in the constructor of B, as:
class A
{
public $name;
public function getName()
{
return $this->name;
}
}
class B extends A
{
public function B()
{
$this->name = 'Prasad';
}
}
$obj = new B();
echo $obj->getName();
This printed Prasad for me using http://writecodeonline.com/php/ to test the code.
Even better, pass the name 'Prasad' when creating the new B object, as:
class A
{
public $name;
public function getName()
{
return $this->name;
}
}
class B extends A
{
public function B( $value = 'Prasad' )
{
$this->name = $value;
}
}
$obj = new B();
echo $obj->getName(), "<br>";
$obj = new B( 'John' );
echo $obj->getName();
It's because you aren't setting the name attribute first. Call B->setName() and then you can get the name by calling B->getName().
I have been browsing some php source code and need to know how the following class and sub methods use works:
<?php
$me = new Person;
$me->name("Franky")->surname("Chanyau")->phone("+22", "456 789");
?>
I have pretty solid knowledge of OOP so I don't want a 101. I just need to know how to make the above code possible.
Method chaining is possible, by
return $this;
at the end of the method.
Explained here:
phpandstuff: Method Chaining Plus Magic Setters
These methods usually set an instance variable and then just return $this.
public function phone($param) {
$this->phone = $param;
return $this;
}
methods name() surname() and phone() return an instance of Person. you can accomplish this by
return $this;
most probably these methods look like this:
public function name($name) {
$this->name = $name;
return $this;
}
like some others said, its a fluid interface http://en.wikipedia.org/wiki/Fluent_interface#PHP the Basic Idea is that a methof of a class always returns the object itself
class Car {
private $speed;
private $color;
private $doors;
public function setSpeed($speed){
$this->speed = $speed;
return $this;
}
public function setColor($color) {
$this->color = $color;
return $this;
}
public function setDoors($doors) {
$this->doors = $doors;
return $this;
}
}
// Fluent interface
$myCar = new Car();
$myCar->setSpeed(100)->setColor('blue')->setDoors(5);
(via wiki)
It's called method chaining. Basically each class function returns the object itself ($this) so that the user can call more functions on the returned object.
public function name() {
//other stuff...
return $this;
}
http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html
http://www.electrictoolbox.com/php-method-chaining
The idea is if we return $this then we can chain the object method calls together. Here's the solution:
<?php
class Person
{
private $strName;
private $strSurname;
private $ArrPhone = array();
public function name($strName)
{
$this->strName = $strName;
return $this; // returns $this i.e Person
}
public function surname($strSurname)
{
$this->strSurname = $strSurname;
return $this; // returns $this i.e Person
}
public function phone()
{ $this->ArrPhone = func_get_args(); //get arguments as array
return $this; // returns $this i.e Person
}
public function __toString()
{
return $this->strName." ".$this->strSurname.", ".implode(" ",$this->ArrPhone);
}
}
$me = new Person;
echo $me->name("Franky")->surname("Chanyau")->phone("+22", "456 789");
?>
Correct answers, but to make the code work you should write:
$me = new Person();
instead of
$me = new Person;