I just discovered that empty() does not work when passing data from an object. Why is that?
This is my code:
// This works
$description = $store->description;
if (!empty($description) )
echo $description;
//This is not working
if (!empty($store->description) )
echo $store->description;
UPDATE
Added extra code for context.
// At the top of my PHP file I have this code
$store = Factory::new_store_obj($id);
// To controll that I got content, I can test by printing object
echo '<pre>';
print_r($store);
echo '</pre>';
//output
Store Object
(
[data:Store:private] => Array
(
[name] => Lacrosse
[street1] => Bygdøy Allé 54
[street2] =>
[zipcode] => 0265
[city] => Oslo
[country] => Norway
[phone] => 22441100
[fax] =>
[email] =>
[opening_hours] =>
[keywords] =>
[description] => Lacrosse er en bla bla bla...
)
)
You should read the docs for empty(). There is an explanation of why empty might fail in the comments.
For example you will get this error if description is a private property, you have set up a magic __get function without the magic __isset function.
So this will fail:
class MyClass {
private $foo = 'foo';
public function __get($var) { return $this->$var; }
}
$inst = new MyClass;
if(empty($inst->foo))
{
print "empty";
}
else
{
print "full";
}
and this will succeed:
class MyClass {
private $foo = 'foo';
public function __get($var) { return $this->$var; }
public function __isset($var) { return isset($this->$var); }
}
$inst = new MyClass;
if(empty($inst->foo))
{
print "empty";
}
else
{
print "full";
}
input:
<?php
$item->description = "testme";
$description = $item->description;
if (!empty($description) )
echo $description;
//This is not working
if (!empty($item->description) )
echo $item->description;
?>
output
testmetestme
conclusion: it works
I tried this:
class test {
private $var = '';
public function doit() {
echo (empty($this->var)) ? 'empty' : 'not';
echo '<br>';
var_dump($this->var);
}
}
$t = new test;
$t->doit();
It outputs: empty, string(0) "". This means that it works. Try it your self if you want. It must be the class context not working.
Related
At the moment I test with this piece of code:
<?php
class Alert {
private $type;
private $message;
public static $_alerts = array();
public function add($type, $message) {
$this->type = $type;
$this->message = $message;
self::$_alerts[] = $this;
}
}
$alert = new Alert();
$alert->add("warning", "test 1");
$alert->add("error", "test 2");
echo "<pre>";
print_r(Alert::$_alerts);
echo "</pre>";
But my results are not like expected:
Array
(
[0] => Alert Object
(
[type:Alert:private] => error
[message:Alert:private] => test 2
)
[1] => Alert Object
(
[type:Alert:private] => error
[message:Alert:private] => test 2
)
)
Why is my added object changed?
Test area: http://codepad.viper-7.com/6q2H2A
That's because your object (i.e. $this in internal context) will be copied by reference, not by value. To do copy by value, you'll need to do:
public function add($type, $message)
{
$this->type = $type;
$this->message = $message;
self::$_alerts[] = clone $this;
}
As an alternative, you'll need to instantiate (that is, for example, constructions like new self - but clone seems to be more flexible here) your object as many times as you'll wish to copy.
By the way, there's easy way to realize what's going on. Use var_dump() instead of print_r() - then you'll see that objects are actually same. Sample for your code (i.e. where copying is not fixed yet):
array(2) {
[0]=>
object(Alert)#1 (2) {
["type":"Alert":private]=>
string(5) "error"
["message":"Alert":private]=>
string(6) "test 2"
}
[1]=>
object(Alert)#1 (2) {
["type":"Alert":private]=>
string(5) "error"
["message":"Alert":private]=>
string(6) "test 2"
}
}
-as you can see, objects are same there.
You save 2 refferences to the same object in your $_alerts array. You need to create a new object for each alert or do something like this:
<?php
class Alert {
private $type;
private $message;
public static $_alerts = array();
private function __construct($type,$message){
$this->type=$type;
$this->message = $message;
}
public function getMessage(){
return $this->message;
}
public function getType(){
return $this->type;
}
public static function add($type, $message) {
self::$_alerts[] = new self($type,$message);
}
}
Alert::add("warning", "test 1");
Alert::add("error", "test 2");
echo "<pre>";
print_r(Alert::$_alerts);
echo "</pre>";
The problem you are seeing is because your code is changing the same object twice.
The first call, it will set the data "warning" and "test 1", and the second time it will overwrite these values.
You can solve this by creating a new instance of the object and adding the data:
$alert = new Alert();
$alert->add("warning", "test 1");
$alert2 = new Alert();
$alert2->add("error", "test 2");
This should give the following result:
Array
(
[0] => Alert Object
(
[type:Alert:private] => warning
[message:Alert:private] => test 1
)
[1] => Alert Object
(
[type:Alert:private] => error
[message:Alert:private] => test 2
)
)
I have a class UserFavorite that extends User.
When i create new object with the class constructor setters cannot set properties.
Constructor:
public function __construct($username, $tabId, $favName, $favUrl = null, $favPosition = null, $favComment = null) {
parent::__construct($username);
$this->tabId = $this->setTabId($tabId);
$this->favName = $this->setFavName($favName);
$this->favUrl = $this->setFavUrl($favUrl);
$this->favPosition = $this->setFavPosition($favPosition);
if ($favComment) {
$this->favComment = $this->setFavComment($favComment);
}
}
Setter:
public function setFavUrl($favUrl) {
$url = filter_var($favUrl, FILTER_VALIDATE_URL);
if (!$url) {
echo $this->showError(...);
exit;
}
echo $url; // THIS LOGS THE URL
$this->favUrl = $url;
}
I creatirng new instance $fav = new UserFavorite($user->getUsername(), 1, 'favorite', 'http://abv.bg', 5, 'mamatisiebalo' );
And when i print $fav i receive :
favorite<pre>UserFavorite Object
(
[favName:UserFavorite:private] =>
[tabId:UserFavorite:private] =>
[favUrl:UserFavorite:private] =>
[favPosition:UserFavorite:private] =>
[favComment:UserFavorite:private] =>
[_favId:UserFavorite:private] =>
[username:protected] => myUserName
[_userId:protected] => 1
)
Any ideas?
You are setting $this->favUrl in the setter function, then overwritting it by assigning the result of the setter function to the same variable.
If you change
$this->favUrl = $this->setFavUrl($favUrl);
To
$this->setFavUrl($favUrl);
You should be OK.
I'm working on a script where I can check permissions and stuff, and I stumbled across something odd.
My permission script can check systems and functions and see if the user is allowed to use m.
I'll explain with some code:
if(permission::check('factbounce', 'magklik')){
echo ' Yep good ';
} else {
echo 'Nope not good';
}
Here I have a system factbounce and a function magklik, now inside the permission class:
public static function check($systemCode, $functionCode = null ){
$instance = self::get();
if($instance->checkSystem($systemCode)){
if(is_null($functionCode))
return true;
if(!$instance->checkSystemFunction($functionCode))
return false;
return true;
}
return false;
}
This piece of code of will trigger checkSystemFunction, and will check on magklik:
private function checkSystemFunction ( $functionCode ){
if(!self::$oSystemFunction)
self::$oSystemFunction = org_system_function::getByCode($functionCode);
if(!is_object(self::$oSystemFunction));
echo 'No Object';
echo '<pre>'.print_r(self::$oSystemFunction, true).'</pre>';
if(!self::$oSystemPermission->checkFunctionPermission(self::$oMedewerker, self::$oSystemFunction))
return false;
return true;
}
The output of the code is this:
No Object
org_system_function Object
(
[primaryKey] => org_system_function_id
[table] => org_system_function
[arAssoc] => Array
(
[function_id] => 1
[name] => May click around
[code] => magklik
[description] => do what you want
)
)
Yep good
I don't understand why it says no object, clearly when I print it it's an object.
You are terminating your if clause with the trailing ;:
if (!is_object(self::$oSystemFunction));
echo 'No Object';
So echo 'No Object' will always get executed.
You're terminating your if statement with a semicolon. So, the echo statement is being processed normally and will get executed every time you run the script.
This should work:
if(!is_object(self::$oSystemFunction))
echo 'No Object';
But it's always a good idea to separate your if else blocks with curly braces.
if(!is_object(self::$oSystemFunction)) {
echo 'No Object';
}
This increases the readabaility and maintainability of your code.
An example to demonstrate this:
$a = 1;
$b = 2;
if ($a == $b);
echo 'foo';
The above code outputs:
foo
See the demo here.
I am developing a web application in PHP,
I need to transfer many objects from server as JSON string, is there any library existing for PHP to convert object to JSON and JSON String to Objec, like Gson library for Java.
This should do the trick!
// convert object => json
$json = json_encode($myObject);
// convert json => object
$obj = json_decode($json);
Here's an example
$foo = new StdClass();
$foo->hello = "world";
$foo->bar = "baz";
$json = json_encode($foo);
echo $json;
//=> {"hello":"world","bar":"baz"}
print_r(json_decode($json));
// stdClass Object
// (
// [hello] => world
// [bar] => baz
// )
If you want the output as an Array instead of an Object, pass true to json_decode
print_r(json_decode($json, true));
// Array
// (
// [hello] => world
// [bar] => baz
// )
More about json_encode()
See also: json_decode()
for more extendability for large scale apps use oop style with encapsulated fields.
Simple way :-
class Fruit implements JsonSerializable {
private $type = 'Apple', $lastEaten = null;
public function __construct() {
$this->lastEaten = new DateTime();
}
public function jsonSerialize() {
return [
'category' => $this->type,
'EatenTime' => $this->lastEaten->format(DateTime::ISO8601)
];
}
}
echo json_encode(new Fruit()); //which outputs:
{"category":"Apple","EatenTime":"2013-01-31T11:17:07-0500"}
Real Gson on PHP :-
http://jmsyst.com/libs/serializer
http://symfony.com/doc/current/components/serializer.html
http://framework.zend.com/manual/current/en/modules/zend.serializer.html
http://fractal.thephpleague.com/ - serialize only
json_decode($json, true);
// the second param being true will return associative array. This one is easy.
PHP8-Code:
class foo{
function __construct(
public $bar,
protected $bat,
private $baz,
){}
function getBar(){return $this->bar;}
function getBat(){return $this->bat;}
function getBaz(){return $this->baz;}
}
//Create Object
$foo = new foo(bar:"bar", bat:"bat", baz:"baz");
//Object => JSON
$fooJSON = json_encode(serialize($foo));
print_r($fooJSON);
// "O:3:\"foo\":3:{s:3:\"bar\";s:3:\"bar\";s:6:\"\u0000*\u0000bat\";s:3:\"bat\";s:8:\"\u0000foo\u0000baz\";s:3:\"baz\";}"
// Important. In order to be able to unserialize() an object, the class of that object needs to be defined.
# More information here: https://www.php.net/manual/en/language.oop5.serialization.php
//JSON => Object
$fooObject = unserialize(json_decode($fooJSON));
print_r($fooObject);
//(
# [bar] => bar
# [bat:protected] => bat
# [baz:foo:private] => baz
# )
//To use some functions or Properties of $fooObject
echo $fooObject->bar;
// bar
echo $fooObject->getBat();
// bat
echo $fooObject->getBaz();
// baz
I made a method to solve this.
My approach is:
1 - Create a abstract class that have a method to convert Objects to Array (including private attr) using Regex.
2 - Convert the returned array to json.
I use this Abstract class as parent of all my domain classes
Class code:
namespace Project\core;
abstract class AbstractEntity {
public function getAvoidedFields() {
return array ();
}
public function toArray() {
$temp = ( array ) $this;
$array = array ();
foreach ( $temp as $k => $v ) {
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
if (in_array ( $k, $this->getAvoidedFields () )) {
$array [$k] = "";
} else {
// if it is an object recursive call
if (is_object ( $v ) && $v instanceof AbstractEntity) {
$array [$k] = $v->toArray();
}
// if its an array pass por each item
if (is_array ( $v )) {
foreach ( $v as $key => $value ) {
if (is_object ( $value ) && $value instanceof AbstractEntity) {
$arrayReturn [$key] = $value->toArray();
} else {
$arrayReturn [$key] = $value;
}
}
$array [$k] = $arrayReturn;
}
// if it is not a array and a object return it
if (! is_object ( $v ) && !is_array ( $v )) {
$array [$k] = $v;
}
}
}
return $array;
}
}
Im new to php and have programmed in other languages. Im trying to solve a certain programming situation: Basically I need to access strings stored inside an object. The internal data structure of the object is an associative array. The values are the strings Im trying to access.
This is the code Im using:
<?php
class OrderAuthenticator
{
private $OrderObj;
public function __construct($Order)
{
echo 'Contructed an instance of Order Authenticator<br/>';
$this->OrderObj = $Order;
echo 'Instantiated OrderContainer<br/>';
}
public function authenticate_Drinks()
{
//echo __LINE__ ;
//4 number or characters including spaces between them
$pattern_drinkName = '([0-9a-zA-Z\s]{1,75})';
//100 characters with spaces allowed between them
$pattern_drinkCalories = '([0-9]{0,3})';
//100 characters with spaces allowed between them
$pattern_drinkCategory = '([0-9A-Za-z\s]{1,50})';
//100 characters with spaces allowed between them
$pattern_drinkDescription = '([0-9A-Za-z\s]{0,300})';
$pattern_drinkPrice = '([0-9.]{1,6})';
//echo __LINE__ ;
$DrinkContainer = $this->OrderObj->getDrinkContainer();
//echo __LINE__ ;
foreach($DrinkContainer as $Drink)
{
//print_r($Drink);
echo __LINE__ ;
echo '<br/>';
}
}
?>
This code produces the following output:
Array (
[0] => Drink Object (
[dataArray:private] => Array (
[drink_name] => SimpleXMLElement Object ( [0] => Gin )
[drink_cals] => SimpleXMLElement Object ( )
[drink_Category] => SimpleXMLElement Object ( [0] => Alocholic )
[drink_desc] => SimpleXMLElement Object ( )
[drink_price] => SimpleXMLElement Object ( [0] => 4.00 )
)
)
)
Now, what I need to do is take the string values out and I need to run a regular expression check on each of those. So I need to store each of these strings in a variable in some kind of a loop.
I had this code trying to do that within the above loop but it didnt work:
$drink_name = $Drink->getName();
echo 'drink name = '.$drink_name.'<br/>';
$drink_calories = $Drink->getCalories();
echo 'drink calories = '.$drink_calories.'<br/>';
$drink_category = $Drink->getCategory();
echo 'drink category = '.$drink_category.'<br/>';
$drink_Description = $Drink->getDescription();
echo 'drink Description = '.$drink_Description.'<br/>';
$Drink_Price = $Drink->getPrice();
echo 'drink Price = '.$Drink_Price.'<br/>';
if(!preg_match($pattern_drinkName, $drink_name))
{
echo __LINE__ ;
return 'Drink name'.$drink_name .' did not match<br/>';
}
else if(!preg_match($pattern_drinkCalories, $drink_calories))
{
echo __LINE__ ;
return 'Drink calories'.$drink_calories .' did not match<br/>';
}
else if(!preg_match($pattern_drinkCategory, $drink_category))
{
echo __LINE__ ;
return 'Drink category'.$drink_category .' did not match<br/>';
}
else if(!preg_match($pattern_drinkDescription, $drink_Description))
{
echo __LINE__ ;
return 'Drink Description'.$drink_Description .' did not match<br/>';
}
else if(!preg_match($pattern_drinkPrice, $Drink_Price))
{
echo __LINE__ ;
return 'Drink Price'.$Drink_Price .' did not match<br/>';
}
else
{
echo __LINE__ ;
return 'Merchant Location input is valid<br/>';
}
Here is the Drink class :
<?php
class Drink
{
private $dataArray;// = array();
public function __construct()
{
echo 'Entered constructor for Drink.php<br/>';
$this->dataArray = array();
}
public function setName($drink_Name)
{
echo 'Added Drink Name to DrinkObj= '.$drink_Name. '<br/>';
$this->dataArray["drink_name"] = $drink_Name;
}
public function getName()
{
echo 'Inside Drink name<br/>';
return $this->dataArray["drink_name"];
}
public function setCalories($drink_Cals)
{
echo 'Added Drink Calories to DrinkObj= '.$drink_Cals. '<br/>';
$this->dataArray["drink_cals"] = $drink_Cals;
}
public function getCalories()
{
return $this->dataArray["drink_cals"];
}
public function setCategory($drink_Category)
{
echo 'Added Drink Category to DrinkObj= '.$drink_Category. '<br/>';
$this->dataArray["drink_Category"] = $drink_Category;
}
public function getCategory()
{
return $this->dataArray["drink_Category"];
}
public function setDescription($drink_Desc)
{
echo 'Added Drink Description to DrinkObj= '.$drink_Desc. '<br/>';
$this->dataArray["drink_desc"] = $drink_Desc;
}
public function getDescription()
{
return $this->dataArray["drink_desc"];
}
public function setPrice($drink_Price)
{
echo 'Added Drink Price to DrinkObj= '.$drink_Price. '<br/>';
$this->dataArray["drink_price"] = $drink_Price;
}
public function getPrice()
{
return $this->dataArray["drink_price"];
}
}
?>
$patterns = array(
'name' => '([0-9a-zA-Z\s]{1,75})',
'calories' => '([0-9]{0,3})',
'category' => '([0-9A-Za-z\s]{1,50})',
'description' => '([0-9A-Za-z\s]{0,300})',
'price' => '([0-9.]{1,6})'
);
$DrinkContainer = $this->OrderObj->getDrinkContainer();
foreach($DrinkContainer as $Drinks)
{
foreach($Drinks as $DrinkObject)
{
$properties = array(
'name' => $DrinkObject->getName(),
'calories' => $DrinkObject->getCalories(),
'category' => $DrinkObject->getCategory(),
'description' => $DrinkObject->getDescription(),
'price' => $DrinkObject->getPrice()
);
foreach($properties as $propname => $propvalue)
{
if(!preg_match($patterns[$propname], $propvalue))
{
return "Drink $propname $propvalue did not match<br/>";
}
}
}
}
In addition to using foreach, as Matt shows, Drink can implement the Iterator or IteratorAggregate interfaces so you can iterate over drinks directly, rather than having to create a second array. It could be as simple as using ArrayIterator to wrap the data array:
<?php
class Drink implements IteratorAggregate {
function getIterator() {
return new ArrayIterator($this->dataArray);
}
#...
or you could write a class to :
<?php
class DataIterator implements Iterator {
protected $data, $idx, $key, $fields;
function __construct($data, $fields = null) {
$this->data = $data;
if ($fields) {
$this->fields = $fields;
} else if (method_exists($data, 'fields')) {
$this->fields = $data->fields();
} else {
throw new InvalidArgumentException(__CLASS__ . ' expects ' . get_class($data) . " to have a 'fields' method, but it doesn't.");
}
}
/*** Iterator ***/
function current() {
return $this->data->{$this->key};
}
function key() {
return $this->key;
}
function next() {
if (++$this->idx < count($this->fields)) {
$this->key = $this->fields[$this->idx];
} else {
$this->key = null;
}
}
function rewind() {
$this->key = $this->fields[$this->idx = 0];
}
function valid() {
return ! is_null($this->key);
}
}
class Drink implements IteratorAggregate {
private $dataArray = array(
'drink_name' => null, 'drink_cals' => null,
'drink_Category' => null, 'drink_desc' => null,
'drink_price' => null
);
function __get($name) {
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->$method();
}
# else return value is undefined. Could also throw an exception.
}
function __set($name, $val) {
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->$method($val);
}
# could throw and exception if $name isn't an accessible property.
}
/* Helps to describe Drinks by returning an array of property names.
*/
function fields() {
return array_keys($this->dataArray);
}
function getIterator() {
return new DataIterator($this);
}
# ...
}
#...
$patterns = array(
'name' => '(^[0-9a-zA-Z\s]{1,75}$)',
'calories' => '(^[0-9]{0,3}$)',
'category' => '(^[0-9A-Za-z\s]{1,50}$)',
'description' => '(^[0-9A-Za-z\s]{0,300}$)',
'price' => '(^[0-9.]{1,6}$)'
);
foreach($drinks as $i => $drink) {
foreach($drink as $propname => $propvalue) {
if(!preg_match($patterns[$propname], $propvalue)) {
return "Drink $i's $propname ('$propvalue') is invalid.";
# or:
//$errors[$i][$propname] = "'$propvalue' is invalid";
}
}
}
Property overloading (__get, __set) isn't necessary for iteration, but does allow for write access within a foreach loop using variable property names (e.g. $drink->$name). Variable property names should be used sparingly as they can obfuscate what property is being accessed, but it's acceptable in a foreach loop because it's clear that every accessible property is being accessed.
You could move validation to the set* methods, throwing exceptions on failure, at which point there would be no need for a validation step.
Notes: <br/> isn't semantic. Often, it should be replaced with paragraph (<p>) elements and the like, using styling to create space. The patterns should be anchored at the start (^) and end ($), otherwise you could get a successful match on just a part of a value, causing validation to succeed when it should fail.