5

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;
3
  • 1
    Why don't you ask the real question? You are obviously trying to use database fields as properties of a class (property != method). This problem has been solved, it's called ORM and there are a few to choose from in world of PHP: Eloquent, Doctrine, Propel. The answer is that you can use as many as you need (thousands) and yes, it will affect the memory and performance but there's too little info to tell you how it's going to do it. Commented Apr 3, 2015 at 8:35
  • i haven't problem with properties , i just want to know about methods, the above code is not realy relevant. Commented Apr 3, 2015 at 11:42
  • 2
    So why even post any code if it's not relevant? PHP lexer will lex the code and translate that to PHP opcodes. Naturally, the more code you have, more opcodes will be created - that takes memory, but not that much memory. The real fun begins when you call those methods - calling a method takes up memory, because each method has to allocate memory for its work and possible output. That means - yes, you can have thousands of methods, it won't really take up any RAM until those methods start doing things - meaning, until you invoke them. Commented Apr 3, 2015 at 12:11

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.