At the first, in your foo.php shouldn't mark your fields Show
Then you should add
then you can create an instantiate of a
and my advice, you should read a little about OOP techniques ;) PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework! Moderator: General Moderators AxllrodForum NewbiePosts: 12Joined: Tue Jun 12, 2012 4:15 pmLocation: Belgium How to extending a class in another fileSo I have been doing some OOP and I hit a snag. I'm trying to extend a class in another php file. The extended classes should load dynamically. I got it set up like this: -->bootstrap.php file Code: Select all
--> and the view.php file Code: Select all
I have been trying lots of different things but I think I am quite close. Does anyone have an idea how to solve this one? Re: How to extending a class in another filePost by mecha_godzilla » Sun Apr 21, 2013 5:21 pm Hi, Do you have error messages switched on in PHP? That is usually a good way to see what's going on if your classes aren't working as expected. A couple of points though: 1. I don't think you can instantiate your "view" class inside your "bootstrap" class, given that "view" is the child of "bootstrap". When I've created page object classes before, what I've done is put some basic URL routing code in my "index.php" script and instantiated my page object in there - my page classes all extend a base class that contains the functionality common to each page object, and I use class autoloading to include the scripts for that particular page class and the base class. I then call a routine in the base class that authenticates the user, generates the menus and *only* after these functions have been completed does it then call the method defined in every page object to carry out the page-specific functions. So, in essence, the way you're trying to organise your classes is correct but the implementation isn't quite right - keep in mind that your "bootstrap" class is only relevant in the context of your "view" class (and therefore there would never be any occasion on which you would want to instantiate the "bootstrap" class directly) and make sure you understand the order in which your object "events" need to happen - for example, the "bootstrap" class can't build the page unless the "view" class has already told it what page template to load. 2. You don't need to redeclare any properties in your "view" class that are already declared in your "bootstrap" class, but you can obviously add new ones if you want to. HTH, Mecha Godzilla Re: How to extending a class in another filePost by mecha_godzilla » Tue Apr 23, 2013 12:56 pm No problem If you need any specific help with your code, just say so and myself and the other forum users will be happy to try and assist. M_G AxllrodForum NewbiePosts: 12Joined: Tue Jun 12, 2012 4:15 pmLocation: Belgium Re: How to extending a class in another filePost by Axllrod » Thu Apr 25, 2013 8:30 am I tried to change my code according as I thought it should happen. However, it didn't do a lot AxllrodForum NewbiePosts: 12Joined: Tue Jun 12, 2012 4:15 pmLocation: Belgium Re: How to extending a class in another filePost by Axllrod » Thu Apr 25, 2013 4:09 pm So I have been playing arround a bit with the information you gave me and suggested and surprisingly I got it to work. The problem is, I don't really know how If somemone could explain to me how the php code does his thing, it would be greatly appreciated. My folder setup is as following: root/ .htacces index.php --->libs/ --->bootstrap.php --->help.php --->index.php The files look like
this: Code: Select all
-->bootstrap.php Code: Select all
-->index.php Code: Select all
-->help.php Code: Select all
All of it works. For example, when i input the url of "http://localhost/testing/nl/index" it then shows the message 'We are in the bootstrap' and 'hello from the index class'. The thing I don't understand is how the autoload function does it's thing. Thanks in advance! Re: How to extending a class in another filePost by mecha_godzilla » Thu Apr 25, 2013 5:41 pm Hi, I think the __autoloader() function is called whenever PHP tries to instantiate an object that doesn't have a class defined for it yet. Under normal circumstances PHP would generate a fatal error if the class you're calling wasn't defined, but the __autoloader() function catches this exception and runs your custom code instead. If the class you're trying to load can't be found in the file that's been loaded, or the file that PHP is trying to load can't be found, a fatal error will still occur though. With that in mind, it would be a good idea to use file_exists() to test for situations where the URL someone is trying to access can't be mapped to a file - you could just create a generic "404" class and load that instead. Just for future reference, the way that you've structured the code means that the bootstrap object and the index/help objects are separate - this may or may not be what you want to achieve, but if they're going to be separate entities then there's no real need for the "child" classes to extend the "parent" (bootstrap) class. I think what I'm trying to say here is that you'll have two instances of the "bootstrap" object running when you could just have one - as I mentioned before, if you instantiate any of the "child" classes they will automatically inherit the properties of the bootstrap class. For production purposes, you might just want to make the "bootstrap" class standalone and have the "child" classes extend a "functions" class instead, so that they can access the shared functions. As an example: -->functions.php Code: Select all
-->help.php Code: Select all
In theory, when you go to "http://localhost/testing/nl/help" your bootstrap class should autoload the "help" class (which will then cause PHP to look for the "functions" class and autoload that) then call the __construct() method defined in the "functions" class to call the hello() method defined in the "help" class. HTH, M_G AxllrodForum NewbiePosts: 12Joined: Tue Jun 12, 2012 4:15 pmLocation: Belgium Re: How to extending a class in another filePost by Axllrod » Fri Apr 26, 2013 5:52 am The thing is, I was thinking about making a sort of application that could handle multiple languages (like english, dutch, french, german...) Depending on the language the site should then loaded dinamically. I started out by making the main class, the bootstrap class, and work from there out. So in the bootstrap class I would then break up the URL and depending on that i would load the controllers needed to generate the requested page. However, how I should handle all of that is still a bit blurry. I was thinking of using a basic MVC structure. There I could have fixed paths to fixed folders, I would only have to fill in the file name with a variable depending on the requested page. How can I access a class from another file in PHP?Use include("class. classname. php");
Can PHP class extend two classes?Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.
How can I inherit in PHP?Inheritance in OOP = When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.
What is the purpose of $this and extends in PHP?Definition and Usage
The extends keyword is used to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.
|