www.webdeveloper.com
Results 1 to 7 of 7

Thread: Class issue

  1. #1
    Join Date
    Oct 2013
    Posts
    9

    Class issue

    Ok so I have this code:

    PHP Code:

    class main{

    function 
    one(){
    echo 
    'something';
    }

    }

    class 
    second extends main{

    function 
    two(){
    echo 
    'something else';
    }

    }

    $var = new main
    Now when I am calling two() like this:

    PHP Code:

    $var
    ->two(); 
    It doesn't work. Is there a way to make it work like the way I have in my code? Because there is more then 1 extended class and I want to be able to use the same variable for everything thats in all extended classes.

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Method two() is not defined in class "main". You need to instantiate "second" if you want that method to be available:
    PHP Code:
    $var = new second();
    $var->one();
    $var->two(); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2013
    Posts
    9
    Sounds good but what if I have another extended class this:


    PHP Code:

    class main{

    function 
    one(){
    echo 
    'something';
    }

    }

    class 
    second extends main{

    function 
    two(){
    echo 
    'something else';
    }

    }

    class 
    third extends main{

    }function 
    three(){
    echo 
    'something different';

    $var = new main

  4. #4
    Join Date
    Oct 2013
    Posts
    9
    oops typo.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Still the same thing: you have to instantiate the class that has the method you want to use.

    The bigger question might then be, what exactly do you want to accomplish with these 3 classes? Maybe what you really want to do is override the main::one() method instead of creating differently named methods in each child class? (Just a possibility. Without knowing what the actual functionality is that you want to accomplish with these simple example classes, I'm only guessing.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Oct 2013
    Posts
    9
    Ok basically this is what I am trying to do:

    I have a file called html.php, in this file it has functions and variables inside the main class. These get called via $var->some_function() ...

    Now I have other files inside a folder called: /html/ Inside these files are classes (The class name is the same as the file name).

    I need to basically loop all these classes to be able to be used in one variable. Is this even possible? Thanks Mate

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    It's still hard to say without really understanding the underlying requirements driving the design. Essentially, you have two options: inheritance or composition. If class second represents a more specific type of class main, then inheritance probably makes sense, so second could extend main, inheriting all of main's methods, overriding some of them as needed, as well as defining additional methods if that makes sense. Therefore, you use "extends" when the child class "is a" particular case of the parent class.

    If, on the other hand, main and second represent different types of objects, then one should not inherit from the other. Instead one could "use" the other, either by instantiating it itself, or have the used object passed into it via the client code. In this case, you might have something like:
    PHP Code:
    <?php

    $var 
    = new Main(new Second(), new Third());
    $var->one();
    $var->second->two();
    $var->third->three();

    class 
    Main
    {
        public 
    $second;
        public 
    $third;

        public function 
    __construct(Second $secondThird $third)
        {
            
    $this->second $second;
            
    $this->third $third;
        }

        public function 
    one()
        {
            echo 
    "<p>one</p>\n";
        }
    }

    class 
    Second
    {
        public function 
    two()
        {
            echo 
    "<p>two</p>\n";
        }
    }

    class 
    Third
    {
        public function 
    three()
        {
            echo 
    "<p>three</p>\n";
        }
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles