Peyman abdollahy
Peyman abdollahy

Reputation: 829

can we define lot of methods in php class?

some body ask this question,but i want to discuse about fuctionality of class. what happend if we define lot of methods, for exapmle 1000,3000 or more line of code in class. does it affect class performance? i mean if we instance a object of class, all of methods go to memory? and it's spend are memory usage? or it's not important at all.

Like this:

class User extends databaseObject{

    protected static $table_name="users";
    protected static $db_fields=array('id','user_type_id','firstname','lastname','password','email',
    'mobile','ostan_id','city_id','tahsilat_id','education_cluster_id','code_meli','birth_date',
    'mobile_verify_code','mail_verify_code','is_instituter','gender_id','active_by','actived_date');

    protected static $required=array('firstname','lastname','password','email','mobile');
    protected static $missing=array();

    public $id;
    public $user_type_id;
    public $firstname="";
    public $lastname="";
    public $password;
    public $email;
    public $mobile;
    public $ostan_id;
    public $city_id;
    public $tahsilat_id;
    public $education_cluster_id;
    public $code_meli;
    public $birth_date;
    public $mobile_verify_code;
    public $mail_verify_code;
    public $is_verified;
    public $is_instituter=0;
    public $active_by;
    public $actived_date;

Upvotes: 5

Views: 157

Answers (2)

smassey
smassey

Reputation: 5931

A non answer: Do not worry about memory usage or performance until you have a performance issue.

A real answer: The number of lines / properties / methods in a class with make no substantial or even visible difference (unless of course your php file starts to weigh in at multiple megabytes, but that would never happen with a few thousand lines of code. In this rare case, it's not PHP that's slowing down, it's simply waiting for disk i/o, there's no way around it). Best way to prove it: benchmark it. You'll see the microsecond and sub-microsecond changes in performance is not worth your time to even attempt to improve. In the end the speed of your application will depend on the data structures you choose and the algorithms you use to interact with them, lines of code is irrelevant.

Last thoughts: as @N.B. commented, it looks like you're building an ORM. There exist many well tested and vastly deployed ORMs already you may want to choose from. Unless this is purely for your learning benefit you're most likely better off using one of them.

Upvotes: 2

KIKO Software
KIKO Software

Reputation: 16761

The methods are only loaded once into memory and then reused whenever you instantiate a class. Only the properties (variables) in a class will take up memory for every object that is created. Your example has lots of properties.

Upvotes: 1

Related Questions