Jump to content

Class building question


Azarian

Recommended Posts

Why is the proper way to call a class is to call a method at the same time?

 

For example

$class = new SomeClass();
$class->some_method();

 

I understand why you would do this but lets say you just have a simple class that you pass some data.  The class than processes the data in some way than returns it.  It is my understanding if you were to make a class likes this.

 

class SomeClass{

     function __construct($data){
          //process data//
          return $data;
    }
}

 

That this code is considered an improper way of doing this because you should never return data from the __construct.  So would this follow code be the proper way to handle this?

 

class SomeClass{

     function __construct($data){
          $this->some_method($data);
    }

     function some_method($data){
          //process data//
          return $data;
    }
}

 

Any good explanation on the proper way to handle this would be greatly appreciated.  When I am making simple classes to do a specific task it would seem more efficient not having to know about any particular method inside the class. 

Link to comment
Share on other sites

Classes are generally designed to store data and functions that relate to processing that data. You use the __construct to instantiate an object (setup it's internal data) then generally call one methods to more to work with that data.

 

If you using classes to simply group functions together (you shouldn't be using them), then you can make your methods static.

 

class Foo {
  public static function Bar() {
    echo "This is Foo::Bar";
  }
}

Foo::Bar();

 

Also, in PHP5.4 you can now call a method without needing to store an object in a variable.

 

(new SomeClass)->someMethod();

 

When I am making simple classes to do a specific task it would seem more efficient not having to know about any particular method inside the class.

 

Then just use a function.

Link to comment
Share on other sites

Classes are generally designed to store data and functions that relate to processing that data. You use the __construct to instantiate an object (setup it's internal data) then generally call one methods to more to work with that data.

 

I get all that, it really doesn't answer my question though.  If all the methods are meant to work with that data only why the extra step why can't it be automated so to speak? 

Link to comment
Share on other sites

I'm really not sure what your getting at. Maybe some pseudo code would help?

 

Lets take the questions in a different direction once and maybe some of these smaller questions will help me solve the bigger question on my own than.

 

What kinda of data is acceptable to pass to the class versus to the method?  Which is the proper way of doing this?

 

Example 1

$class = new SomeClass($data);
$class->some_method();

 

or

 

Example 2

$class = new SomeClass();
$class->some_method($data);

 

In example 1 your passing the data to the construct to be setup for the entire class to use.  Than you still have to call the method that processes the data. 

 

Example 2 you pass the data directly to the method but you have other supporting methods in that class that need that data too.

 

Maybe I am not getting when to use and not to use the __construct method or using it improperly? 

Link to comment
Share on other sites

You generally don't make a class which only has one function to process the data.  In those cases you'd just use a regular function.

 

Classes have several functions which all do various things to the data.  You pass into the constructor any data that the whole class will need.  Individual functions would be passed any extra data that they might need.

 

Generally you avoid doing any significant amount of processing in the constructor because it makes it easier to extend a class if you do the processing in methods which can be called.

 

... because you should never return data from the __construct.

 

You can't return anything from a constructor.  The constructor is just for setting up the class by assigning properties or any initialization work.  The "return value" is always the newly created object.

 

Link to comment
Share on other sites

Azarian, in answer to your question regarding where you should really put the data and as an extension to what has already been said, anything out through the construct function would generally be static information assigned to internal private variables which are used later on when executing other functions within the class.

 

For example, an SQL class may include the database information in the construct function as a parameter which gets assigned to an internal variable. This information could then be called when executing your connect method. If you have multiple sets of database details, they can all be loaded into the SQL class on instantiation and called individually on an "as needed" basis.

 

Hopefully that makes sense?

Link to comment
Share on other sites

I been thinking this over for the last few days and I think the design of my classes is flawed.  I think I am having them do to much.  With classes our end game with them is to create an object.  What should the scope of the object be?  Are we trying to make every piece of data a object or can an object be a whole dataset?  Lets say for example were dealing with a login system and we have a username and pass.  What would the best way to go about this to have a class that deals with the login and returns back if it was a success or not?  Do we break it down farther and have a class for the username, class for pass, and a class that check.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.