View Full Version : Resolved 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
$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
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
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 :o
Kind regards
Dal
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:
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.
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 :D. 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 :D Sorry: spend all my time over a programming talk but planning on using this board more as it seems more active ;)
-----
Dirt_Diver
01-10-2009, 05:15 AM
Edit your original post and ten click on advanced edit. From there you can add the sub title of RESOLVED
Ah ha! Thanks Dirt_Driver :D
Dirt_Diver
01-10-2009, 05:20 AM
Ah ha! Thanks Dirt_Driver :D
Sure NP
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 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. :D
Kind regards
Dal
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.
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
You're probably thinking of MFC, the Microsoft Foundation Classes, a Windows development toolkit. That's independent of the compiler used — you could equally compile a plain C++ application, or use another library like Qt or plain Win32.
hmm, never knew these things when I was trying and help was never around when I needed it (I used yahoo chat as my main source of help and people on there, well most were just horney teens and the rooms were filled with filth, never knew anything like dynamic forums or programming talk existed back in 1999-2000, I was also on dial-up 56k which was running about half that because of a telephone line issue I was having :()
Why is M$ always the bane of my existence!
You'll probably find better information in ##C++ on irc.freenode.org, although beware: they can be a bit crotchety. :) http://www.catb.org/~esr/faqs/smart-questions.html will help.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.