A Zend-Framework project that uses Doctrine.
Data comes in form of objects. In my Zend View i access it like
$this->personData->getPersonAdress()->getPersonStreet();
Since its possible that a Person doesnt have an associated adress, we have to check if the personadress exists and if the personStreet is filled before echoing because otherwise an echoing NULL error may occur.
So we use some IFs with isset:
<? if($this->personData->getPersonaddress()) echo $this->personData->getPersonaddress()->getPersonstreet(); else echo "''"?>
Example (worst case):
<?
if(isset($this->address[0]) && is_object($this->address[0]))
{
$help2=$this->address[0]->getAddress();
if (isset($help2) && is_object($help2))
{
$help=$this->address[0]->getAddress()->getCountry();
if (isset($help) && is_object($help) && $help->getCountryId())
{
echo $this->address[0]->getAddress()->getCountry()->getCountryId();
}
}
}
?>
We need a solution or eventualla a Zend_view helper to simplify the procedure of echoing these values.
Any ideas would be highly appreciated..
You can tell Doctrine to return an array, instead of mapped objects, by using HYDRATE_ARRAY.
That way, isset() can be called directly.
I solved the issue myself by implementing a Zend View Helper that prints out every value ignoring errors that could occur by non-object properties or NULL associations.
This should be useful to everyone working with Zend Framework + Doctrine 2.
Usage
Instead of
$this->address[0]->getAddress()->getCountry()->getCountryId()
Use (it delivers the value or default (0, third parameter) if not set)
$this->Printsafe($this->address[0], "getAddress/getCountry/getCountryId", 0)
Code following
class Printsafe extends Zend_View_Helper_Abstract {
public function isObjectOrSet($data, $properties)
{
if ($data != null)
{
if(is_object($data))
{
if (isset($properties[0]))
{
$actual_property = array_shift($properties);
return $this->isObjectOrSet($data->{$actual_property}(), $properties);
}
}
elseif(isset($data))
{
return $data;
}
else
return null;
}
else
return null;
}
/**
* Wants data and getters + properties
* Tests if they are set and returns requested value
* Returns empty value (instead of NULL) if any error occurs
*
* #param mixed $data - Example $this->personData
* #param string $properties - Example "getPersontype/getNotation"
* #param mixed $default - return value if not set. Default is empty string.
*
* #return mixed $value
*/
public function printsafe($data = null, $properties='', $default = '') {
$return = null;
if ($data != null)
{
if ($properties != '')
{
$prop_array = explode("/", $properties);
$return = $this->isObjectOrSet($data, $prop_array);
}
}
if ($return == null)
$return = $default;
return $return;
}
}
You could prefix the chain with #, which suppresses error output - then you wouldn't need to check it at all. If it doesn't exist, it will simply not output anything.
<?php echo #$this->personData->getPersonAdress()->getPersonStreet(); ?>
It isn't usually recommended to use the # operator, but in this case it seems like an appropriate solution. (The disadvantage is that you would miss other errors that may come from this line)
Related
I am loading table data fetched using Ajax in a table with datatables. The logic fetches some value from the .env file using getenv() function. This getenv() is returning false only some of the times and otherwise not.
I tried opening the URL (HTTP GET) which datatables use to fetch the data thru Ajax in a browser, and there it does not seem a problem at all.
public function ap_FetchData()
{
$accountLibrary = new Account();
if($accountLibrary->checkIsUserLogged())
{
// do some stuff and print JSON - dummy logic
$data = [];
for($i=1;$i<=10;$i++)
{
$image = getBucketURL($i.".jpg");
$data[] = $image;
}
outPutAsJSON($data);
}
else outPutAsJSON("Your session is timed-out. Please login again.", 403);
}
Some times I get the JSON output with the Bucket URLs (partial) and sometimes I get session is timed-out error. However, this sessions timed-out behavior is seen only on the AJAX.
The checkIsUserLogged is at Account.php at Libraries directory
function checkIsUserLogged(): bool
{
$sessionName = getenv("session.name"); // this is the session name stored in env file sometimes and false sometimes
// do validation and return true or false
}
The getBucketURL() is at a helper file.
function getBucketURL($key)
{
return getenv("s3bucket.url").$key;
/*
* some times this return as http://www.example.com/$key
* and
* some times it return only $key
* var_dump(getenv("s3bucket.url")) outputs (bool) false is second case
*/
}
Expected output:
{
"http://www.example.com/1.jpg",
"http://www.example.com/2.jpg",
"http://www.example.com/3.jpg",
.
.
"http://www.example.com/10.jpg"
}
Actual problematic Output:
{
"1.jpg",
"2.jpg",
"3.jpg",
.
.
"10.jpg"
}
Edit
$_SERVER["s3bucket.url], $_ENV["s3bucket.url] and $_SERVER["session.name], $_ENV["session.name] works perfect. Seems the problem is only with getenv()
For a more 'bulletproof' solution, use CodeIgniter 4's helper function:
env(string $key, $default = null).
I.e:
echo env("s3bucket.url");
It first searches for the 'key' in the $_ENV variable, then the $_SERVER environment variable and lastly, the getenv(string $varname, bool $local_only = false) PHP function.
EXCERPT FROM:
https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Common.php
if (! function_exists('env')) {
/**
* Allows user to retrieve values from the environment
* variables that have been set. Especially useful for
* retrieving values set from the .env file for
* use in config files.
*
* #param string|null $default
*
* #return bool|string|null
*/
function env(string $key, $default = null)
{
$value = $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key);
// Not found? Return the default value
if ($value === false) {
return $default;
}
// Handle any boolean values
switch (strtolower($value)) {
case 'true':
return true;
case 'false':
return false;
case 'empty':
return '';
case 'null':
return null;
}
return $value;
}
}
I have a bunch of optional settings and I'm sick of checking for isset and property_exists.
In Laravel, if I ask for a property that does not exist on a model or request, I get null and no complaints (errors). How can I do the same for my data structure.
If I try array, I can't do simple $settings['setting13'], I have to either pre-fill it all with nulls or do isset($settings['setting13']) ? $settings['setting13'] : '' or $settings['setting13'] ?? null. If I try an object (new \stdClass()), $settings->setting13 still gives me a warning of Undefined property.
How can I make a class such that it responds null or an empty string whenever it is asked for a property that it doesn't have?
Simply do what Laravel does, create a class that deals with your data structure which returns a value if key exists, and something else if it doesn't.
I'll illustrate with an example class (this class supports the "dot notation" of accessing array keys):
class MyConfigClass
{
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function get($path = '', $default = null)
{
if(!is_string($path))
{
return $default;
}
// There's a dot in the path, traverse the array
if(false !== strpos('.', $path))
{
// Find the segments delimited by dot
$segments = explode('.', $path);
$result = $this->data;
foreach($segments as $segment)
{
if(isset($result[$segment]))
{
// We have the segment
$result = $result[$segment];
}
else
{
// The segment isn't there, return default value
return $default;
}
}
return $result;
}
// The above didn't yield a result, check if the key exists in the array and if not - return default
return isset($this->data[$path]) ? $this->data[$path] : $default;
}
}
Use:
$my_structure = [
'url' => 'www.stackoverflow.com',
'questions' => [
'title' => 'this is test title'
]
];
$config = new MyConfigClass($my_structure);
echo $config->get('url'); // echoes www.stackoverflow.com
echo $config->get('questions.title'); // echoes this is test title
echo $config->get('bad key that is not there'); // returns null
There is also a possibility to create wrapper as Jon Stirling mentioned in a comments. This approach will allow to keep code clean and also add functionality via inheritance.
<?php
class myArray implements ArrayAccess {
private $container;
function __construct($myArray){
$this->container = $myArray;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$settings = array("setting1"=>1,"setting2"=>2,"setting3"=>3);
$arr = new myArray($settings);
echo $arr['setting1'];
echo "<br>";
echo $arr['setting3'];
echo "<br>";
echo $arr['setting2'];
echo "<br>";
echo "------";
echo "<br>";
echo $arr['setting4'] ?:"Value is null";
!empty($settings['setting13']) ? $settings['setting13'] : ''
can be replaced with
$settings['setting13'] ?: ''
as long as whatever you want to print and whatever you want to check exists is the same expression. It's not the cleanest thing ever - which would be to check the existence of anything - but it's reasonably clear and can be chained :
echo ($a ?: $b ?: $c ? $default ?: '');
However, you are not the first who are "sick of checking for isset and property_exists, it's just that we still have to do it, or else we get unexpected results when we expect it the least.
It's not about saving time typing code, it's about saving time not debugging.
EDIT : As pointed in the comments, I wrote the first line with isset() instead of !empty(). Since ?: returns the left operand if it's equal to true, it's of course uncompatible with unchecked variables, you have at least to check for existence beforehand. It's emptiness that can be tested.
The operator that returns its left operand if it exists and is different from NULL is ??, which can be chained the same way ?: does.
Admittedly not the best way to do this, but you can use the error suppressor in php like this:
$value = #$settings['setting13'];
This will quitely set$value to NULL if $settings['setting13'] is not set and not report the undefined variable notice.
As for objects, you should just calling for attributes that are not defined in class.
I'm creating a php class which is getting slightly out of hand the deeper it gets.
Here's an example:
unset($this->file[$key]->inspect->formats);
unset($this->file[$key]->inspect->tags);
unset($this->file[$key]->inspect->chapters);
unset($this->file[$key]->inspect->annotations);
unset($this->file[$key]->inspect->automatic_captions);
unset($this->file[$key]->inspect->subtitles);
$this->file[$key]->inspect->name = trim($this->file[$key]->inspect->name);
$this->file[$key]->inspect->artist = trim($this->file[$key]->inspect->artist);
Instead of writing $this->file[$key]->inspect for every single variable I want to use is there a way I can set a variable e.g $inspect to take this place?
So that when I write $inspect->subtitles it'll know what I really mean and affect the main $this->file[$key]->inspect->subtitles?
$inspect = &$this->file[$key]->inspect;
declare this. Now you can set your data like this
$inspect->formats = 'format';
$inspect->subs = 'subs';
// ...
adding the & you will affect the variable and not only a copy of this variable
here are explanations about references http://php.net/manual/en/language.references.whatare.php
One approach you could use is replicate the object_get() helper method from Laravel that will fetch elements based on dot notation.
/**
* Get an item from an object using "dot" notation.
*
* #param object $object
* #param string $key
* #param mixed $default
* #return mixed
*/
function object_get($object, $key, $default = null)
{
if (is_null($key) || trim($key) == '') return $object;
foreach (explode('.', $key) as $segment)
{
if ( ! is_object($object) || ! isset($object->{$segment}))
{
return value($default);
}
$object = $object->{$segment};
}
return $object;
}
$ob = new StdClass();
$ob->property->name->value = 'Lol';
echo object_get($ob, 'property.name.value');
Unfortunately there'd be a bit of extra implementation if the $object->property was an array like in your example.
Is there a way I can have my PHP function return a different value type?
The following code should explain what I mean:
<?php
public function test($value){
if($value == 1){
return "SUCCESS";
} else {
return false;
}
}
?>
On one condition I'm returning a string, otherwise I'm returning a Boolean. Is that allowed/possible?
Yes. PHP is loosely typed, so any variable can take any type. Function return values are no exception.
Yes, it's possible (but not advisable).
You should at least declare it in your Javadoc comment
/**
* #param int $value
* #return mixed string|boolean
**/
public function test($value){
if($value == 1){
return "SUCCESS";
} else {
return false;
}
}
Even PHP's built in functions do it sometimes (e.g mysql_connect => boolean | resource link)
It is allowed. However, it is not advisable in the case you gave. For an example where this makes sense, take a look at e.g. strpos() function.
Yes, that's allowed, not like in C, Java, ..., where you have to define the return.
I wonder about this myself, why return something that is different? So the receiving end gets two things to check?
I think I have settled on returning an empty version of the type or an array with a status element myself.
Two examples, how things can change within a function/method, and you are basically forced to check more than expected:
<?php
function test($value) {
if ($value == 1) {
$local = successFunction($value);
return $local;
}
else {
return false;
}
}
$res = test(1);
if ($res) {
echo "Are we OK? Right? No? What is this?";
}
Version 2
function test($value) {
if ($value == 1) {
$local = successFunction($value);
return $local;
}
else {
return "";
}
}
$res = test(1);
if (!empty($res)) {
echo "More likely we are OK.";
}
?>
Is that allowed/possible?
Yep.
I find in my PHP pages I end up with lines and lines of code that look like this:
$my_id = isset($_REQUEST['my_id']) ? $_REQUEST['my_id'] : '';
$another_var = isset($_REQUEST['another_var']) ? $_REQUEST['another_var'] : 42;
...
Is there a better, more concise, or more readable way to check this array and assign them to a local variable if they exist or apply a default if they don't?
EDIT: I don't want to use register_globals() - I'd still have the isset problem anyway.
How about wrapping it in a function?
<?php
function getPost($name, $default = null) {
return isset($_POST[$name]) ? $_POST[$name] : $default;
}
a better method might be to create a singleton/static class to abstract away the details of checking the request data.
Something like:
class Request {
private $defaults = array();
private static $_instance = false;
function getInstance () {
if (!self::$_instance) {
$c = __CLASS__;
self::$_instance = new $c;
}
return self::$_instance;
}
function setDefaults($defaults) {
$this->defaults = $defaults;
}
public function __get($field) {
if (isset($_REQUEST[$field]) && !empty($_REQUEST[$field])) {
return $_REQUEST['field'];
} elseif (isset($this->defaults[$field])) {
return $this->defaults[$field];
} else {
return ''; # define a default value here.
}
}
}
you can then do:
# get an instance of the request
$request = Request::getInstance();
# pass in defaults.
$request->setDefaults(array('name'=>'Please Specify'));
# access properties
echo $request->name;
echo $request->email;
I think this makes your individual scripts loads cleaner and abstracts away the validation etc. Plus loads of scope with this design to extend it/add alternate behaviours, add more complicated default handling etc etc.
First, use $_POST for POSTed variables. $_REQUEST is a mashup of many different incoming variables, not just $_POST and could cause problems.
One solution for your question would be to create a function that handles the isset() logic.
function ForceIncomingValue($Key, $Default) {
if (!isset($_POST[$Key]))
return $Default;
else return $_POST[$Key];
}
first of all, NEVER use the $_REQUEST variable, it'll lead to bugs and other problems during development
function getPOST($key) {
if(isset($_POST[$key])) {
return $_POST[$key];
}
}
note that this code leaves the variable empty when $_POST[$key] was not set
you could also adapt that code to enable you to instead provide you with a (sensible) default when the value could not be loaded.
function getPOST($key, $default = NULL) {
if(isset($_POST[$key])) {
return $_POST[$key];
} else {
return $default;
}
}
Is the set of variables you're expecting known at the time of the script's writing, or do you want to do this for an arbitrary set of values? If the former is true, you could do something like this:
# This array would hold the names of all the variables you're expecting
# and a default value for that variable name
$variableNames = array (...);
foreach ($variableNames as $key => $default) {
if (isset ($_REQUEST[$key])) $$key = $_REQUEST[$key];
else $$key = $default;
}
Basically, this takes advantage of PHP's ability to evaluate variables to create other variables (hence the double-dollar for $$key--this means create a new variable whose name is the value of $key).
I haven't yet come up with a good solution to the latter situation.
PHP's null coalescing operator!
$username = $_GET['user'] ?? 'nobody';
For a lot of variables, with a requirement check, anyone is free to use my expect function.