#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    A few PHP OOP questions


    Hi;

    I have got a few OOP questions.

    1 - What is the right way of adding requirements to existing software? For example in a bank account class there is a new feature: Interest rate.

    Is it a better idea to change the current code or write new code from start?

    Personally it makes more sense to me to write new code from start. If that is the case then what happens to very detailed coming up issues? For example naming? If my current class is called "Bank_account", now the new class is called "Bank_account_with_interest_rate"? I believe there is no global rule of choosing class name. Maybe it's better practise to call it "B_a_w_r" and add comment //Bank account with interest rate?

    The other thing is the URLs. In CI for example then mention:
    A Controller is simply a class file that is named in a way that can be associated with a URI.
    Code:
    Consider this URI:
    example.com/index.php/blog/  
    In the above example, CodeIgniter would attempt to find a controller named blog.php and load it.
    Does this mean I need to change my routing and mod re-write? Or I need to change all hyper links on the website?




    2 -Does this make sense to say an object should have only 1 responsibility, 1 reason to exist and that reason is entirely encapsulated within 1 class; it can have multiple methods but all those methods are oriented around that reason for existence?



    Dependency inversion principle and Liskov Sub Principle
    Can someone give a real life example of one of each?


    Abstract classes:
    Can I say it exists only for the purpose of being inherited. Never gets instantiated. It exists only to provide shared behaviours. For example a "Bank_account" class can be abstract. Other classes extend from it like Savings_account, Cheque_account etc.

    Interfaces
    A collection of methods and properties. (classes implement from an interface)
    If class implements an interface it means they MUST provide all methods mentioned in the interface.
    I want to know why interfaces? Is it to force a structure?
    Can someone please provide a real life example of a class that both extends and implements?

    Static
    Can I say static property is a property that its value is shared between all objects. For example interest rate of a Savings_account. All object have the same value for that property.


    Thank you Jesus
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76
    You shouldn't need to change the code, if this is true oop code, just make a new class file that handles all of the interest rate functions and include it where it's needed in the class file that handles the bank details

    Also look up PHP Namespaces on how to have to classes with the same name included or used in the same file

    For the rest of your questions, I will let someone else explain those to you
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by English Breakfast Tea
    1 - What is the right way of adding requirements to existing software? For example in a bank account class there is a new feature: Interest rate.

    Is it a better idea to change the current code or write new code from start?
    Like always, it depends. Is it only a small feature for an existing class? Then you simply add a new method. Is it a complete set of features which constituates a whole new "entity"? Then you create a subclass. Did your customer throw away the whole concept and now wants something completely different? Then you rewrite the code.

    Just so that there's no misunderstanding: You certainly do not create a new subclass every time you have a new feature. This makes no sense. The class tree depends on the functionalities you wanna implement, not on the time you've implemented them. You don't want hundreds of useless subclasses "BankAccountNowWithNewAttributes", "BankAccountWithImprovedConstructor", ...



    Originally Posted by English Breakfast Tea
    If my current class is called "Bank_account", now the new class is called "Bank_account_with_interest_rate"? I believe there is no global rule of choosing class name.
    The rule is that class names should cut to the chase. What's the meaning of the class? Is there a good English word for it?

    In your code, I guess "with interest rate" is an OK name. At least I can't think of a better one.

    You should also stick to one of the mainstream naming conventions.



    Originally Posted by English Breakfast Tea
    Maybe it's better practise to call it "B_a_w_r" and add comment //Bank account with interest rate?
    Nooo. Why would you do that? The code should be understandable by itself. Constantly looking up cryptic abbrevations is one of the most awful things I would think of. Everybody who has to work on the code (including your future self) will hate you for that.



    Originally Posted by English Breakfast Tea
    Or I need to change all hyper links on the website?
    Why do you need to change the URL at all? I mean, a blog is a blog. If your blog gets new functionalities, you simply add new features to the Blog class. You don't rename the class or something.



    Originally Posted by English Breakfast Tea
    2 -Does this make sense to say an object should have only 1 responsibility, 1 reason to exist and that reason is entirely encapsulated within 1 class; it can have multiple methods but all those methods are oriented around that reason for existence?
    Yes.



    Originally Posted by English Breakfast Tea
    Abstract classes:
    Can I say it exists only for the purpose of being inherited. Never gets instantiated. It exists only to provide shared behaviours. For example a "Bank_account" class can be abstract. Other classes extend from it like Savings_account, Cheque_account etc.
    Yes.



    Originally Posted by English Breakfast Tea
    Interfaces
    A collection of methods and properties. (classes implement from an interface)
    If class implements an interface it means they MUST provide all methods mentioned in the interface.
    I want to know why interfaces? Is it to force a structure?
    Can someone please provide a real life example of a class that both extends and implements?
    An interface is an abstract specification. It specifies which methods a class should have, but it doesn't say how those are implemented.

    For example, a file, a server etc. are all input-output entities. They all have certain functionalities like "read" and "write". However, you cannot actually define those methods in a generic superclass, because how exactly the reading and writing works depends entirely on the concrete entity. What now? You can simply do nothing and keep the classes as unrelated entities. Or you can use an interface to specify the common functionalities.

    PHP itself also uses interfaces. For example, if your class implements the Iterator interface, you can use a foreach loop to iterate through its "elements" (whatever those are).



    Originally Posted by English Breakfast Tea
    Static
    Can I say static property is a property that its value is shared between all objects. For example interest rate of a Savings_account. All object have the same value for that property.
    A static property belongs to the class rather than a particular instance. It's a feature of the class.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,408
    Rep Power
    1031
    The rule of thumb I use for object properties is pretty much what Jacques has already said. If it's just a simple value then it can be a simple object property. If the value requires functionality of some sort (such as calculations or formatting for display) you "may" want it in a separate class depending on just how complex the processing code is.

    If all you want to do is format a value for display such as a phone number or a date then it can still be an object property and upon displaying the data you run it through a utility function to format it. Something like interest rate however might need a separate class because you might want to do calculations on it. Those caclulation routines would be methods of the InterestRate class in that case. The interestRate property of the loan class would then be an instance of the InterestRate class.

IMN logo majestic logo threadwatch logo seochat tools logo