Results 1 to 9 of 9

Thread: Oop

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default Oop

    when writing php class is it important that the class name is the same as the file name?

    Code:
    <?php
    class Person{
    
    	var $name = "Stefan";
    	
    	function get_name(){
    		return $this->name;
    	}
    }
    ?>
    should this file be called Person.php?

    also, when would you use -> vs = ?


    oops, i thought i was in php

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    class names vs. class file names
    no, it's not important. it might help you keep track of what you're doing, however.

    -> vs. =
    these are very different operators. As you probably know already, the equal sign ( = ) assigns a value to a variable - as in $var = 'value'; or $result = what_some_function_returns(); .

    The arrow ( -> ) is used to access properties and methods inside a class. (In OOP, a function inside a class is called a method, and a variable inside a class is called a property.)

    Take this, for example:
    PHP Code:
    $person = new Person(); // using "=" to assign the class to a variable
    $name $person->get_name(); // also using "->" to access a method in the class

    echo $person->name;  // this works
    echo $person=name;  // this does not

    class Person{

    public 
    $name 'Stefan';  // this is a "property" (note: "public" is treated the same as "var")

       
    public function get_name(){
          return 
    $this->name;
       }



  3. The Following User Says Thank You to traq For This Useful Post:

    ggalan (10-24-2010)

  4. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    so if class A would like to get a property in class B, would you recommend using "include" to reference outside classes?

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Include is irrelevant. Include takes the code from one file and "moves" it into another (when processing-- nothing permanent of course). There is no difference between an included bit of code and another bit of code written directly into the page. (The only exception is the __FILE__ constant that will be different based on which file it is used in.)
    The big advantage of include is organization-- split up the code into files that can be handled more easily than dealing with one really long file. And it also helps to reuse code in several pages: pages A and B can both include C and thereby share resources.

    To do what you are describing, you would need to create an instance of B within A, probably, or reference a global variable (instance of the class). And you also must have the method/variable be public within B, so that A can access it.

    Another way to approach this is to create subclasses (extensions) or just make A a bigger class.

    The best method really depends on what you're trying to accomplish.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    so if i had 2 separate class, how would i access 'get_city' from the class Person?
    Code:
    class Person{
    
    public $name = 'Stefan'; 
    
       public function get_name(){
          return $this->name;
       }
    }
    Code:
    class Place{
    
    public $city = 'd.c.'; 
    
       public function get_city(){
          return $this->city;
       }
    }

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In their most basic sense, classes are groups of functions (and variables).

    Generally speaking, if "get_city" and "get_person" are related functions, you'd put them in the same class.

    Your example above is very simple and I don't see any real purpose for it, so I'm not sure what you are trying to test out-- I know you're going to do something else with it, but from that information I can't see an obvious goal.

    Code:
    class Person{
    
    public $name = 'Stefan'; 
    public $home = 'd.c.'; 
    
       public function get_name(){
          return $this->name;
       }
       public function get_city(){
          return $this->home;
       }
    }

    But again, that's not going much for you.


    The way that I think about classes is this: if I have a group of related functions that I want to act as a unit, I put them together in a class. And in the "object" sense, another reason is if you have a "thing" that you want to do several things to. For example, you could create a "person" class then make them live somewhere, then echo where they live then make them move to another city. And perhaps they can have children and pay taxes. All of these would be inside the same "person" class. Of course you can relate several classes if you need to, for example if "city" is a complex idea or, as a simpler example, the children themselves are people.


    It's kinda like a database with cross-referencing but less about data and more about operations-- functions.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i understand that you can group similar methods, the point of my question was 1 class accessing methods and properties from another class and the best was to handle that

  9. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    couple methods:

    1) create an instance of the second class within the first:
    PHP Code:
    class Person{

    public 
    $name 'Stefan';
    public 
    $city;

    public function 
    get_city(){
       
    $cityclass = new City();
       
    $this->city $cityclass->get_city();
       return 
    $this->city;
    }


    2) create an instance of the second class outside the first:
    PHP Code:

    $cityclass 
    = new City();
    $city $cityclass->get_city();

    class 
    Person{

    public function 
    get_persons_city(){
       global 
    $city;
       return 
    $city;
    }


    3) have the city class extend the person class:
    PHP Code:
    class City extends Person{

    public 
    $city='Springfield';

    public function 
    get_city(){
       return 
    $this->city();
    }

    public function 
    get_name(){
       return 
    parent::get_name();
    }



  10. #9
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    awesome, thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •