Php oop classes call inside functions?

For your webapp write an autoloader that is just loading all classes from files you want to use while you need it.

For your webapp write an autoloader that is just loading all classes from files you want to use while you need it. You can then just write the code like you want to w/o the need to build complex inheritance between classes only because you want to make use of the functionality you've coded. This has multiple benefits: Even w/o much experience in OOAD you keep stuff apart from each other by not introducing much relationship between multiple classes.

Relationship between classes can be bad because you make things dependable on each other when it's not really needed. You can easily extend and change your application. That's important as you continue to code, be it now or later.

That's just my 2 cents. If you don't like autoloader later on, you can change that, too. But it helps to get things on the run straight-forward.

Example: function my_autoloader_function($name) { require(sprintf('%s/%s. Php', $yourlibfolderdir, $name)); } spl_autoload_register('my_autoloader_function'); // ... (even in other files) $obj = new Test5; $obj->funcZalabri(); The registered autoloader function will be called each time a class is instantiated/accesses while it does not exists. In the example it's assumed that there is one class per file which is quite common and in my opinion very nice to manage the code.As the autoloader is a callback and it's written in PHP you can do whatever you want in it and make it much more detailed to fit your needs.

Hope this helps.

– fxuser Jun 7 '11 at 5:56 @fxuser: you don't have to. This function will be automatically called by PHP itself with the name of the class that should be loaded. You just code your load logic into your autoloader then.

– hakre Jun 7 '11 at 8:04 ok and how do I make it get more classes than 1 if I have to use more than 1? Add multiple requires or store filenames in array and do a foreach to require them all... – fxuser Jun 7 '11 at 8:16 no if you have more classes than 1, the function will be called more than once: Each time with the name of the class that is to be loaded. See here for PHP Autoloading basic principles, I use spl_register_autoload() because it allows to use it together with other autoloaders (e.g. When a library needs autoloading as well).

– hakre Jun 7 '11 at 8:33.

No, it's pretty good to do it that way. The only advice I can give you is to assign variables like this: $modelClass = new ModelClass(); But you need to change your listing to: Class Test{ public function wheels(){ return 'wheels'; } } Class Test2{ public function bikes(){ $test = new Test(); return 'i am a bike with '. $test->wheels(); } }.

Seriously thats how I do it , its just 2am and I can barely see waht im typing lol. Thanks though – fxuser Jun 6 '11 at 23:03 No problem. One more tip - if you'll need this variable more than once - make it a class property and instantiate in constructor or pass to a setter.

– Tomasz Kowalczyk Jun 6 '11 at 23:04.

That's the conventional practice in OO in general, not just in PHP. If you need $test in more than just Test2.bikes(), then you should declare it as a global variable to that Class Test2.

That is how you normally do it. Although, if you need the new class in other functions within the class, you probably want to instantiate it once outside the functions instead of creating a new object every time.

This depends on the class that you need, and how you need it and why you need it. If you need a fresh copy, each time the function runs, you might want to do it your way. If you don't need a new copy (this is what you are doing, which if you need the clean copy, you are doing it right!), but just need access to some independent method, you might consider making the sub class an child element of your class.

For example, if you were doing a more .net style OOP you could have something like this. Which has both styles of OOP. I realize this isn't an efficient or correct way of representing the DOM, but this is an example.

Class Entity { private $children = array(); public addChild(Entity $entity) { ... } public getChild($index) { ... } public removeChild($index) { ... } } class Text : Entity //this is inheritence { private $text; public setText($text) { ... } public getText() { ... } } class Attributable : Entity { protected $attributes = array(); public setAttribute($key, $value) { ... } public clearAttribute($key) { ... } public getAttribute($key) { ... } } class Option : Attributable { public $text = new Text(); //this is a child element } class Select : Attributable { portected $options = array(); public addOption(Option $entity) { ... } public getOption($index) { ... } public removeOption($index) { ... } }.

Your code is certainly legal (aside from needing to call $test->wheels as Tomasz menitioned) For many OOP languages, however, you specify the contents in the constructor and then use them in the method call: Class Test2{ private $test; function __construct($test) { $this->test = $test; } public function bikes(){ return 'i am a bike with '. $this->test->wheels(); } } This is particularly true if you want to dynamically change the wheels.

– fxuser Jun 6 '11 at 23:08 @fxuser Yes (or any other class that has a wheels() method) – Kathy Van Stone Jun 10 '11 at 14:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions