开发者

How do I ensure a method exists before calling it?

A user and a company can log in to my site.

They each have their own object. So user will have $users = new Use开发者_StackOverflow中文版rs() defined, and company has $place = new Places()..

Now this works fine there is a page where both should see the content. Inside this page there is a method which checks whether $users->id() is the same as the one you are logged in with. Works fine, but when you log in with company, then it will throw error as $users is not defined.

What should I do about this? Should i just set the $users to false and then check out from this? Or have something to determine what youre logged in with before this statement so it wont execute.. idea?


It sounds like you have a basic conceptual problem.

I'd suggest rather than having two classes - users and companies - have instead a single user class, with a 'company' being simply a type of user (or a derived class). Then you wont have code sprinkled with checks to see which objects are valid. It'd greatly simplify application logic.


It sounds like a Company in this context is just a specialized type of User.

If a Company type user has a similar model to the User type user, then I'd extend Company off of User to implement the additional functionality.

class Company extends User
{
    ...
}

However, if the two types are dissimilar and the only commonality they have is that you can login with them, I'd create a base class that can be used for logging in and for the ID attribute, then have their separate models define the rest.

abstract class BaseUser
{
    public function getId()
    {
        return $this->id;
    }

    protected $id = null;
}

class User extends BaseUser
{
    // user specific stuff here
}

class Company extends BaseUser
{
    // company specific stuff here
}


it's hard to give a definite answer without more context. But basically you need to check whether the $users object exists before calling one of it's methods. There are many ways to do this, the simplest is probably:

if (is_object($users)) {
  // use the object
}


Alternatively, you could change your structure a little, and go for a generic user model, with a field that defines what type of logon it is:

<?php
    public class User
    {
        /* Data fields */
        public $logonType = "USER"; // or "COMPANY"
    }
?>

Using that, you don't have to worry about what type of object is being passed around, you can simply check the field to determine the logon type, unless your two models have drastically different data


In addition to Mike's comment you can use Factory for creating a specific user depending on type.

class Factory 
{
    public static function create($type='user')
    {
        switch ($type) {
            case 'user':
                return new User();
            break;
            case 'company':
                return new Company();
            break;
            default:
                throw new Exception('Invalid type');
            break;
        }  
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜