Repeated functions between classes

How do you all handle cases where you have two classes, but have two functions within each that are identical? Is this ok? The function is basically creating unique keys for different tasks.

using namespaces might be the answer?

How so?

This sounds like you might want to abstract the functionality into its own class, which you can then inject / pass into the classes that need it.

2 Likes

I, so far, have 2 functions that I know will need to be interchangeable. Should I just do something like class Miscellaneous{} or something? Create instances as needed…? I autoload classes. Will I need to include the autoload function file (bootstrap.php) inside the class.php file?

E.g. I have something like process.php. That loads up bootstrap.php which is the autoloader. In process.php I create the class crap that I need.

If I’m going to, in each main class, create an instance of the Miscellaneous class in the class.php files, will I need to include the bootstrap.php file to autoload? Not sure whether process.php including autoload will cascade down into the class.php file I’m initiating via autoload…

My knowledge of classes is limited and for better or worse I would create a third MY_ParentClass with a function identical() that Class One and Class Two can extend.

// Class C_One.php

<?php // Class C_One.php

include ( 'libs/MY_ParentClass.php' );

class C_One extends  MY_ParentClass
{

function index()
{
  $params = 'Specific to Class_One';
  $uniqueKeys = $this->identical( $params );
  ...
  ...
}

}//endClass

// Class C_Two.php

<?php  // Class C_Two.php

include ( 'librs/MY_ParentClass.php' );

class C_Two extends  MY_ParentClass
{

//=============================================
function index()
{
  $params = 'Specific to Class_Two';
  $uniqueKeys = $this->identical( $params );
  ...
  ...
}

}//endClass

Can someone with more experience confirm whether it would be best to extend, like in this case? Extending is basically allowing a class to get functions/methods of another class? Right?

If so, this would be perfect.

Also, does MY_parent class, in that case, need a construct method? I assume that each class.php file that I want to extend has to include the bootstrap.php (autoload) file? Instead of doing require_once?

Whether you want to extend a class or use a class is going to depend on your specific circumstances and whether there’s a “is-a” or a “has-a” relationship. So we’ll have to see the code we’re talking about to give more specific advice.

1 Like

So far I have a UserServices class, and a Subscribers class (UserServices is for handling login stuff/logout/register (yes I know this breaks SRP. Haven’t gotten around to fixing this.), and Subscribers will handle new subscribers.)

Each one of these needs the ability to create authentication keys. Users needs it for “remember me” authkey, Subscribers need it for their activation code in the e-mail.

So far, I just repeat the function in both classes (email isn’t created yet, so I’m purely talking about the auth key.)

Both will eventually need an e-mail function which will send specific e-mails based on what I need. There will probably be more situations like this.

If you still need to see code, let me know. I hope I’ve explained it well enough though to not provide code.

Those both definitely aren’t an “is-a” relationship. A user service “is a” auth key? Nope. A subscriber “is a” auth key? Nope. But they both use an auth key. So instead you could make an AuthenticationKey class, and pass an instance into both the user services and subscribers classes.

I assume the same for creating an e-mail class? Seems sort of silly to have a one method class. Is this normal?

Also, is there some sort of shortcut for the following?

$user=new User();
$user->methodThing();

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.