How to Echo a Custom Object in PHP

How to echo a custom object in PHP?

class TheClass {
public $Name;
public $Number;
function MrFunction() { /* bla bla bla */ }

public function __toString()
{
return $this->Name . ' '. $this->Number;
}
}

echo $theClassInstance;

How to echo an object?

Since your class doesn't have a __toString() method, you cannot echo the class object itself.

So you have a few alternatives here, either declare a __toString() method that prints what you want it to, or print the variables separately.

Example using the magic-method __toString() (demo at https://3v4l.org/NPp2L) - you can now echo $class.

public function __tostring() {
return "A2 is '".$this->a2."' and B2 is '".$this->b2."'";
}

Alternative two is to not print the class itself, but get the properties of the class instead. Since they are public, you don't need a getter-method to use them in the public scope.

$class = new A();
$class->mohamed('name')->test('mohamed');;

echo $class->a2." and ".$class->b2;

Demo at https://3v4l.org/nHeP9

What happens when a PHP object is echoed?

Magic method __toString() in your class. This allows your class to execute the code in that method when your object is treated like a string (i.e. when used with echo). The method must return a string, otherwise it will raise an error.

You will notice in the library you linked, that they have one in their form class.

/**
* Convert to HTML
*/
public function __toString()
{
return $this->getHtml();
}

How to print all properties of an object

<?php var_dump(obj) ?>

or

<?php print_r(obj) ?>

These are the same things you use for arrays too.

These will show protected and private properties of objects with PHP 5. Static class members will not be shown according to the manual.

If you want to know the member methods you can use get_class_methods():

$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name)
{
echo "$method_name<br/>";
}

Related stuff:

get_object_vars()

get_class_vars()

get_class() <-- for the name of the instance

php create object without class

you can always use new stdClass(). Example code:

   $object = new stdClass();
$object->property = 'Here we go';

var_dump($object);
/*
outputs:

object(stdClass)#2 (1) {
["property"]=>
string(10) "Here we go"
}
*/

Also as of PHP 5.4 you can get same output with:

$object = (object) ['property' => 'Here we go'];

Echo a custom page's custom field outside the loop

Try this :

<?php

global $wp_query;

$postid = $wp_query->post->ID;

echo get_post_meta($postid, 'Your-Custom-Field', true);

wp_reset_query();

?>

Echo CustomField from nested array object in PHP

The moment you do this:

foreach($result->response->CustomFields as $CustomField) {

You can access the fields like this:

$CustomField->Key
$CustomField->Value

So if you want to process it further, you can do for example something like this:

$someClass->{$CustomField->Key} = $CustomField->Value

Which translates to: $someClass->Phone = "12345678" . It pretty much depends on what you really want to do next with the values.

Adding a custom function to an already instantiated object in PHP?

Here is a simple and limited monkey-patch-like class for PHP. Methods added to the class instance must take the object reference ($this) as their first parameter, python-style.
Also, constructs like parent and self won't work.

OTOH, it allows you to patch any callback type into the class.

class Monkey {

private $_overload = "";
private static $_static = "";

public function addMethod($name, $callback) {
$this->_overload[$name] = $callback;
}

public function __call($name, $arguments) {
if(isset($this->_overload[$name])) {
array_unshift($arguments, $this);
return call_user_func_array($this->_overload[$name], $arguments);
/* alternatively, if you prefer an argument array instead of an argument list (in the function)
return call_user_func($this->_overload[$name], $this, $arguments);
*/
} else {
throw new Exception("No registered method called ".__CLASS__."::".$name);
}
}

/* static method calling only works in PHP 5.3.0 and later */
public static function addStaticMethod($name, $callback) {
$this->_static[$name] = $callback;
}

public static function __callStatic($name, $arguments) {
if(isset($this->_static[$name])) {
return call_user_func($this->_static[$name], $arguments);
/* alternatively, if you prefer an argument list instead of an argument array (in the function)
return call_user_func_array($this->_static[$name], $arguments);
*/
} else {
throw new Exception("No registered method called ".__CLASS__."::".$name);
}
}

}

/* note, defined outside the class */
function patch($this, $arg1, $arg2) {
echo "Arguments $arg1 and $arg2\n";
}

$m = new Monkey();
$m->addMethod("patch", "patch");
$m->patch("one", "two");

/* any callback type works. This will apply `get_class_methods` to the $m object. Quite useless, but fun. */
$m->addMethod("inspect", "get_class_methods");
echo implode("\n", $m->inspect())."\n";

PHP custom object casting

You don't need to cast the object, you can just use it as if it was a product.

$name = $objectToSave->Name;

get type in php returns object and not the object type

Just to explain why gettype() doesn't work as expected since others have already provided the correct answer. gettype() returns the type of variable — i.e. boolean, integer, double, string, array, object, resource, NULL or unknown type (cf. the gettype() manual link above).

In your case the variable $campaign is an object (as returned by gettype()), and that object is an instance of the class Campaign (as returned by get_class()).



Related Topics



Leave a reply



Submit