I have a class where before most methods (not all), I check if an id is valid. The purpose of each method in the class is to check various user details and each method returns true or false so other classes can check things.
public function doSomething($userId){
$valid = false;
if(empty($userId)){
return $valid;
}
//do other stuff which may turn valid true
return $valid;
}
So to tidy up the class I have moved this check from all the methods it appears in, to it's own method.
public function doSomething($userId){
$valid = false;
$this->idCheck($userId);
//do other stuff
return $valid;
}
private function idCheck($userId){
if(empty($userId)){
return false;
}
return true;
}
If the id check fails, how could I get the method - doSomething to return? Would it be a further check inside this method:
if(!$this->idCheck($userId))return false;
Or is there a better way?
You did it the right way. Just return your function if the ID is invalid.
Otherwise - and thats even better, check your ID in the constructor!
public function __construct($userID)
{
if(!empty($userID))
{
//do constructing things
}
}
As DarkBee mentioned, you could rewrite your idCheck function into following:
private function idCheck($userID)
{
return empty($userID);
}
I would do something like this:
private function idCheck($userId)
{
$status=true;
if(empty($userId))
{
$status=false;
}
return $status;
}
Related
Im not so experienced in php , Im using codeigniter to write my application , I have my own library and within my library there are three functions/methods that passes there arguments in one function which is in one of my models , my question is how will i manipulate/trick the method in my model to know exactly which function among the three in my library has passed the value and return the correct value ..
1st function
public function id_exist ($id) {
if(empty($id)) {
return FALSE;
}
return $this->library_model->this_exist($id);
}
2nd function
public function group_exist($group) {
if(empty($group)){
return FALSE;
}
return $this->library_model->this_exist($group);
}
with the 3rd same as the above 2
in my model
public function this_exist ($item) {
if(empty($item) || !isset($item)) {
return FALSE;
}
// here is where i need to know which function has passed the argument so that i can work with it and return the correct value from the database
}
Might be dirty, might be not sophisticated, but why not passing another argument which tells exactly the origin?
public function id_exist ($id) {
if(empty($id)) {
return FALSE;
}
return $this->library_model->this_exist('id', $id);
}
public function group_exist($group) {
if(empty($group)){
return FALSE;
}
return $this->library_model->this_exist('group', $group);
}
Model:
public function this_exist ($origin, $item) {
if(empty($item) || !isset($item)) {
return FALSE;
}
if($origin == 'id'){
// do something
}
elseif($origin == 'group') {
// do something else
}
}
I am trying to make a class what will contain a set of properties. it will be used like so:
$class = new test_class();
$class->property;
$class->second_property;
Basically, if the properties exist, then they are true, if the properties do not exist, they are false. The properties have no value, only existence.
Now, I want to do something like this:
$class = new test_class();
var_dump($class->property); // false - property does not exist
var_dump($class->second_property); // true - second_property exists
var_dump( (bool) $class); // true
So if even one property of the test class exists, var dumping the $class will show true because it is an object.
However, in the situation where the class has no properties, I want this to happen:
$class = new test_class();
var_dump($class->property); // false - property does not exist
var_dump($class->second_property); // false - second_property does not exist
var_dump( (bool) $class); // false
But, I still want $class to be instanceof the test_class but return false in a logic test.
Is this at all possible? If so, how would I do it?
Thanks, Ozzy
edit:
To clarify, I am already using the __get() magic function. What I want to happen is if the test_class has no properties, then when a var_dump is performed on it, it returns false but an instanceof will return test_class.
elaborating...
I am creating a complex permissions system.
A user gets assigned sections and each section has a set of permissions.
It will work like this:
$user = permissions::get_user(USER_ID_HERE);
// Every property of the $user is a section
var_dump($user->this_section_exists); // true - returns an object
var_dump($user->this_section_doesnt); // false - returns a boolean
If a section exists, then it returns an object of the sections permissions.
var_dump($user->this_section_exists); // true
var_dump($user->this_section_exists->this_permission_exists); // true
var_dump($user->this_section_exists->this_permission_doesnt); // false
Heres the edge case:
var_dump($user->this_section_doesnt); // false
var_dump($user->this_section_doesnt->some_permission);
// This should also return false, which it does,
// But it throws a notice: "Trying to get property of non-object"
I want to be able to either suppress that notice without any modifications to the code which calls the class, ie, no # to suppress.. or be able to return an object with no properties which evaluates to false on a logic test.
What I want to happen is if the test_class has no properties, then when a var_dump is performed on it, it returns false but an instanceof will return test_class.
That is not possible, var_dump will always show you the correct type, that's what it was made for.
No, you can't do it exactly as you wrote it. But you can emulate something similar:
class A {
public function __toString() { // this is what you want, but it only works when you use your object as a string, not as a bool.
if (count(get_class_vars(__CLASS__))) { // so if the class has at least 1 attribute, it will return 1.
return '1';
} else {
return '0';
}
}
}
$a = new A;
var_dump((bool)(string)$a); // false
If I add a propery in A class it will return true. You can also use it without (bool).
if ((string)$a) {
echo 'I am not empty';
} else {
echo 'I am an empty object';
}
If you don't want to use (string), you have one option left, to create a method that contains the code from __toString() and call it instead of casting.
References:
__toString()
get_class_vars()
__CLASS__
PS: About what you said - doing a var_dump() on the object to return false. No, that is not possible.
You can do some wrapping with __get() to manipulate the answer.
Say something like
class test_class {
private $_validProperties;
public function __construct()
{
$this->validProperties = array('foo', 'bar', 'widget');
}
public function __get($prop)
{
if (isset($this->_validProperties[$prop])
{
return true;
}
return false;
}
}
$a = new test_class();
var_dump($a->foo); //true
var_dump($a->tree); //false
You can try PHP magic function
<?php
class Test {
private $name = 'name';
public $age = 20;
public function __get($name) {
return isset($this->$name) ? true : false;
}
public function __toString() {
return true;
}
}
$object = new Test();
var_dump($object->name); // output `true`
var_dump($object->age); // output `20`
var_dump($object->notfound); // output `false`
var_dump((bool)$object); // output `true`
If I understand you correctly, I can only do so.
Hope it can help you
<?php
class Permissions {
private $userPermissions = array(
1 => array(
'Blog' => array('Post'),
),
);
private $permission;
public function __construct($id) {
$this->permission = $this->userPermissions[$id];
}
public function __get($name) {
if(isset($this->permission[$name])) {
return new $name($this->permission[$name]);
}
return new Notfound();
}
}
class Blog {
private $permission;
public function __construct($permission) {
$this->permission = $permission;
}
public function __get($name) {
if(($key = array_search($name, $this->permission)) !== false) {
return new $name();
}
return new Notfound();
}
public function __tostring() {
return true;
}
}
class Post {
public function __get($name) {
return isset($this->$name) ? true : new Notfound();
}
public function __tostring() {
return true;
}
}
class Notfound {
public function __get($name) {
return false;
}
public function __tostring() {
return false;
}
}
$user = new Permissions(1);
var_dump('Blog ', $user->Blog); // return Blog
var_dump('Blog:Post', $user->Blog->Post); // return Post
var_dump('News', $user->News); // return Notfound
var_dump('News:Post', $user->News->Post); // return false
I'm working on activecollab custom module's permissions, and getting this error message when try to calling function of static method dont know why; please do help will be really appericiatable ..
Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in D:\wamp\www\activecollab\public\activecollab\3.0.9\modules\projectcomrequest\models\Projectcomrequests.class.php on line 130
the code I did in model file is:
class Projectrequests extends DataManager {
...
....
function getPermissionValue($name){
$roles = Roles::find();
foreach($roles as $role) {
if($role->getPermissionValue($name))
return true;
else
return false;
}
static function canAccess() {
if(self::getPermissionValue('can_use_project_request')) return true;
return false;
} // canAccess
...
..
}
calling in controller by this:
echo Projectrequests::canAccess();
foreach($roles as $role) {
if($role->getPermissionValue($name))
return true;
else
return false;
You're missing a closing } there. So it should be:
class Projectrequests extends DataManager {
...
....
function getPermissionValue($name){
$roles = Roles::find();
foreach($roles as $role) {
if($role->getPermissionValue($name))
return true;
else
return false;
} // <-- here
}
static function canAccess() {
if(self::getPermissionValue('can_use_project_request')) return true;
return false;
} // canAccess
...
..
}
A static method doesn't have a class context $this as you try to call in the first line of canAccess(). You should call self:: instead of $this-> to access the class context and then you can only call other static field and methods. You will have to make getPermissionValue also static.
A few more errors:
You forgot a { in your foreach. Fixed this for you (only return true inside the loop, the else construction is useless because otherwise your foreach only loops once).
You can immediately return the value of the call to getPermissionValue in canAccess since it is a boolean anyway (the if-else construction is kind of useless).
Corrected code:
static function getPermissionValue($name){
$roles = Roles::find();
foreach($roles as $role) {
if($role->getPermissionValue($name))
return true;
}
return false;
}
static function canAccess() {
return self::getPermissionValue('can_use_project_request');
} // canAccess
I would like to advice as well to use access modifiers like public and private as it is good practice.
<?php
class Projectrequests extends DataManager {
...
....
function getPermissionValue($name){
$roles = Roles::find();
foreach($roles as $role) {
if($role->getPermissionValue($name))
return true;
else
return false;
} // <!---- YOUR ERROR IS HERE
}
static function canAccess() {
if($this->getPermissionValue('can_use_project_request')) return true;
return false;
} // canAccess
...
..
}
Also, static methods do not have access to $this you need to use self:: instead
I have a validation class which uses method chaining. I would like to be able to do single checks with TRUE/FALSE like this:
if ($obj->checkSomething()) {}
But also chain methods like this:
if ($obj->checkSomething()->checkSomethingElse()) {}
The problem however is that if one method returns FALSE, it will not send back an object and thus breaks the method chaining which ends with this error:
Fatal error: Call to a member function checkSomething() on a non-object in ...
Do I have to pick either single method return calls or method chaining or is there a workaround?
One idea would be to set an internal flag to indicate success or failure, and access it via another method, while checking that flag in each method and not doing anything if it's set. E.g.:
class A {
private $valid = true;
public function check1() {
if (!$this->valid) {
return $this;
}
if (!/* do actual checking here */) {
$this->valid = false;
}
return $this;
}
public function check2() {
if (!$this->valid) {
return $this;
}
if (!/* do actual checking here */) {
$this->valid = false;
}
return $this;
}
public function isValid() {
return $this->valid;
}
}
// usage:
$a = new A();
if (!$a->check1()->check2()->isValid()) {
echo "error";
}
To minimize the boilerplate checking in each function, you could also use the magic method __call(). E.g.:
class A {
private $valid;
public function __call($name, $args) {
if ($this->valid) {
$this->valid = call_user_func_array("do" . $name, $args);
}
return $this;
}
private function docheck1() {
return /* do actual checking here, return true or false */;
}
private function docheck2() {
return /* do actual checking here, return true or false */;
}
public isValid() {
return $this->valid;
}
}
The usage would be same as above:
$a = new A();
if (!$a->check1()->check2()->isValid()) {
echo "error";
}
I believe you're looking to have an instance evaluate as true/false based on the outcome of validation.
While some languages allow you to override the boolean value of an instance, php does not (except for casting to string, that is. See PHP's Magic Methods).
Also, the booleans page in the PHP Manual has a list of things that evaluate to false, but it doesn't give any method of overriding the behavior either.
That being said, I'd suggest going with JRL's idea, and construct a chain of validation rules, then 'execute' it with a function that returns the boolean needed in your if statement.
You could wrap them up in a subclass, perhaps.
e.g. if you have
class Validate {
public function checkSomething($data) {
if ($data === $valid) {
return true;
}
return false;
}
public function checkSomethingElse($data) {
if ($data === $valid) {
return true;
}
return false;
}
}
You could do this:
class ValidateChain extends Validate {
protected $valid = true;
public function checkSomething($data) {
if (false === parent::checkSomething($data)) {
$this->valid = false;
}
return $this;
}
public function checkSomethingElse($data) {
if (false === parent::checkSomethingElse($data)) {
$this->valid = false;
}
return $this;
}
public function getIsValid() {
return $this->valid;
}
}
$v = new ValidationChain();
$valid = $v->checkSomething()->checkSomethingElse()->getIsValid();
Quick and dirty, E&OE. And you'd probably need to add a way to find out which bits weren't valid, etc.
Is there a way I can check if a function is true inside another function?
For example.
function Name() {
//check database
}
function Send() {
if(Name() == TRUE){
//do something
}
}
Sure: just make sure the function returns TRUE:
function Name() {
echo 'Some Name';
return TRUE;
}
function Send() {
if (Name() == TRUE) {
// Do something
}
}
*Edit: What Mark Trapp said. ;)
That should work. You would just need to return true; or return false; in the Name() function and you should be set.