1

so i have a class which contains basic stuff like

Class Test{
   public function wheels(){
        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?

4
  • 1
    Why do you believe the above might not be good practise? Commented Jun 6, 2011 at 22:58
  • There is not really enough information on what you are trying to accomplish in order to give you guidelines on best practice. Commented Jun 6, 2011 at 23:00
  • 1
    i dont know, i have build a web app all self taught and since i dont have any experience... i am now trying to improve it. Commented Jun 6, 2011 at 23:00
  • 1
    @Petah, i am trying to call classes to get some required functions from other classes to do some stuff inside another functions of different classs Commented Jun 6, 2011 at 23:01

6 Answers 6

2

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();
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

seriously thats how i do it , its just 2am and i can barely see waht im typing lol. thanks though
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.
1

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:

  1. 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.
  2. 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.

4 Comments

so i put the autoloader and spl autoloader register in a file and how do i make $name param to have more than 1 classes in it? using an array maybe?
@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.
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...
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).
0

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.

Comments

0

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.

Comments

0

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) { ... }
}

Comments

0

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.

2 Comments

in this example $test inside constructor is the Test1 class right?
@fxuser Yes (or any other class that has a wheels() method)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.