php oop classes call inside functions
so i have a class which contains basic stuff like
Class Test{
public function wheels(){
开发者_StackOverflow echo 'wheels';
}
}
and another one
Class Test2{
public function bikes(){
$test = new Test();
return 'i am a bike with '.$test;
}
}
so my question is if i have different classes that i need inside a function is it good practise to call it like $var = new ClassName();
or there is a better way to call different classes inside classes functions?
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();
}
}
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.
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.
精彩评论