Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Basic OOP question

  1. #1
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default Basic OOP question

    I've come across one issue which I'm not clear on;
    The two examples below should in my eyes have the same output but they dont, the last example errors with it unable to find the class for some reason.

    EXAMPLE 1

    index.php
    PHP Code:
    <?php

        $test 
    = new Testing;


        class 
    Controller
        
    {
            public function 
    Controller()
            {
                echo 
    "Auto Load Controller Method<br>";
            }
            public function 
    Display_Msg_From_Controller()
            {
                echo 
    "Displaying Message from Controller Method<br>";
            }
        }
        
        
        
        class 
    Testing extends Controller
        
    {        
            function 
    Testing()
            {
                
    parent::Controller();
                
    $this->Display_Msg_From_Controller();
            }
        }

    ?>
    Outputs;
    Auto Load Controller Method
    Displaying Message from Controller Method


    EXAMPLE 2 : Controller class has been placed in an external file and included instead.

    index.php
    PHP Code:
    <?php

        
    include "controller.php"//Contains Controller class (see below)

        
    $test = new Testing;

        class 
    Testing extends Controller
        
    {        
            function 
    Testing()
            {
                
    parent::Controller();
                
    $this->Display_Msg_From_Controller();
            }
        }

    ?>
    external file controller.php
    PHP Code:
    <?php

        
    class Controller
        
    {
            public function 
    Controller()
            {
                echo 
    "Auto Load Controller Method<br>";
            }
            public function 
    Display_Msg_From_Controller()
            {
                echo 
    "Displaying Message from Controller Method<br>";
            }
        }

    ?>
    Outputs;
    Fatal error: Class 'Testing' not found in C:\xampp\htdocs\testx\index.php on line 4
    Why should it fail to find the class Testing when all I did was make Controller an include??? :s

    Kind regards
    Dal
    Last edited by Dal; 01-10-2009 at 05:17 AM.
    Programmers are tools used to convert Caffeine to code

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, yes, this one's a bit subtle. There are actually two passes made on a PHP file. Firstly, top-level classes and functions are scanned and preloaded, then the scanning starts from the top and the rest of the script is executed. This is the behaviour that allows you to write:
    Code:
    foo();
    
    function foo() { }
    However, when you move the class Controller to a separate file, it can no longer be preloaded. Since Testing depends on Controller, Testing can't be preloaded either, so you don't get this behaviour.

    There are a few other circumstances that cause this, so you're better off making it a rule of thumb to always define things before you use them, in PHP and other imperative languages.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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

    Dal (01-10-2009)

  4. #3
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default

    Thanks Twey I thought I was loosing my marbles

    I took it for granted since I never see a compiler with php so never need to worry about the magic stuff going on over at the server . I'm sure when I was writing C++ code I remember coming across something like this but unfortunatly that was a long time ago and I'm pretty new to OOP... (never really had an excuse to use it, wish I had now, could have saved me a lot of messy code in the past ) ...so these errors are going to confuse me for a while I think!

    Thanks for the reply

    PS: I actually corrected the problem by including both as external scripts which solved the problem but your explaination has answered my question "why"!

    Thanks again.
    Dal

    -----
    EDIT: Err.... I think I've already asked this on here before but where is the mark as solved (or resolved) button... this needs to be closed Sorry: spend all my time over a programming talk but planning on using this board more as it seems more active
    -----
    Programmers are tools used to convert Caffeine to code

  5. #4
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    Edit your original post and ten click on advanced edit. From there you can add the sub title of RESOLVED

  6. The Following User Says Thank You to Dirt_Diver For This Useful Post:

    Dal (01-10-2009)

  7. #5
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default

    Ah ha! Thanks Dirt_Driver
    Programmers are tools used to convert Caffeine to code

  8. #6
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    Quote Originally Posted by Dal View Post
    Ah ha! Thanks Dirt_Driver
    Sure NP

  9. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm sure when I was writing C++ code I remember coming across something like this but unfortunatly that was a long time ago and I'm pretty new to OOP...
    That's quite impressive, since the whole point of C++ is that it's object-oriented
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #8
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default

    Twey: I know, I guess I never really understood the benefits of OOP as the examples in books would always show me how to do "hello world" the long way around with OOP and since this should be 1 line with 1 keyword I got stuck in my own stuborness I guess. Doors have opened now and I can safely say that I have a firm grasp of programming before I use OOP techniques, I guess thats a good thing because I seem to be picking the concepts up fast... still need help with a lot of it but you get my point.

    Can I also point out that it was Borland C++ I was using and didn't really feel like sticking with it since everything had moved to visual C++ by the time I became comfortable with it. Have baught a book about 3 years ago for Visual C++ but just havnt found the time. If I can learn OOP in php then I guess the principals are the same regardless of the language (well anything based on C++ structure anyway). Got on to other stuff (networking and pc hardware) and moved onto a VB project (that was a pain, quite a horrid little language) then perl then php and javascript, covering java next and trying to pick up code igniter framework although the template stucture doesnt seem to be present with all code going directly into the html documents.

    Currently creating my own little framework (based heavily on Code Igniter, well from the parts I understand), so far I have the URI pointing to the class and then the method which is a neat way of doing things but I've introduced my own template handlers against my little project and although there is plenty still to do, it's looking all quite neat and tidy although I realise that Code Igniters structor is alot more superiour, I am learning both the OOP and how I can use the CI framework successfully

    Hope that provides you with a little of why Im in the situation I am.

    Kind regards
    Dal
    Programmers are tools used to convert Caffeine to code

  11. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Can I also point out that it was Borland C++ I was using and didn't really feel like sticking with it since everything had moved to visual C++ by the time I became comfortable with it.
    Doesn't make much difference. Apart from a few compiler-specific things, mostly pragmas and the like, the language you'd use is exactly the same.
    If I can learn OOP in php then I guess the principals are the same regardless of the language (well anything based on C++ structure anyway).
    PHP's OO is actually based on Java's, although it has been further simplified and of course the type systems are very different. The version used in C++ is quite a bit more complex. If you're looking to learn OO, I suggest starting with Common Lisp and CLOS, which is widely held to be the most powerful OO system in existence right now. C# might be better than Java for your purposes, too: it's both more featureful and more familiar to someone from a C/C++ background, and is gathering speed in areas where Java previously held sway.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  12. The Following User Says Thank You to Twey For This Useful Post:

    Dal (01-11-2009)

  13. #10
    Join Date
    Jul 2008
    Posts
    102
    Thanks
    36
    Thanked 6 Times in 6 Posts

    Default

    Doesn't make much difference. Apart from a few compiler-specific things, mostly pragmas and the like, the language you'd use is exactly the same.
    I remember downloading a copy of M$ VC++ compiler and well, not knowing where to start and what all the funny windows classes where. Like I say, moved onto new things. My mission was to break into games programming but i think I was born a little too late into becoming a bedroom programmer. The web is the new frontier of bedroom programming and I guess it will never be "owned" by the big corporations.

    "Common Lisp and CLOS"
    - Great, 2 more to lookup!

    Kind regards
    Dal
    Programmers are tools used to convert Caffeine to code

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
  •