3

I have the following three objects which includes the following methods and properties:

$obj1
 - $prop1
 - $prop2
 - $prop3
 - $prop4
 - method1()
 - method2()
 - method3()
 - method4()

$obj2
 - $prop2
 - $prop3
 - $prop4
 - $prop5
 - method2()
 - method3()
 - method4()
 - method5()

$obj3
 - $prop1
 - $prop3
 - $prop4
 - $prop5
 - method1()
 - method3()
 - method4()
 - method5()

All properties and methods of a given name utilize identical script to create them.

One possible script to create these objects is as follows:

class class1 { 
  var $prop1=123, $prop2=array(), $prop3, $prop4;
  public method method1($x) {echo('hello '.$x;}
  public method method2($x) {echo('goodby '.$x;}
  public method method3($x) {echo('hey '.$x;}
  public method method4($x) {echo('seeya '.$x;}
}

class class2 { 
  var $prop2=array(), $prop3, $prop4, $prop5=555,;
  public method method2($x) {echo('goodby '.$x;}
  public method method3($x) {echo('hey '.$x;}
  public method method4($x) {echo('seeya '.$x;}
  public method method5($x) {echo('latter '.$x;}
}

class class3 { 
  var $prop1=123,  $prop3, $prop4, $prop5=555,;
  public method method1($x) {echo('hello '.$x;}
  public method method3($x) {echo('hey '.$x;}
  public method method4($x) {echo('seeya '.$x;}
  public method method5($x) {echo('latter '.$x;}
}

How can I create these three objects without duplicating the script used to create the properties and methods?

13
  • What are you asking? We need more info than this. What do you mean by "utilize identical script" and "without duplicating the script"? How exactly are you creating these and what exactly are you trying to do instead? Commented Sep 3, 2015 at 18:56
  • 3
    Sounds like a good excuse to read about Traits Commented Sep 3, 2015 at 18:56
  • 1
    @MarkBaker The same thought came into my head when I read the title, but when I read the question it looks more like an xy problem Commented Sep 3, 2015 at 18:59
  • 1
    Could you use the 'property and method' creation scripts inside the individual `factories' that create these objects? It would allow re-use of the 'generation' code? How complex is the 'generation code'? I am not sure that 'inheritance' is the way to go when creating your objects as they are very different from each other. Commented Sep 3, 2015 at 19:16
  • 1
    If you wanted to use them in common scenarios then I would suggest using interfaces for the individual methods such as method1 etc. That way, you can pass them about for common processing for the individual methods, even though they are very different objects but with common processing requirements? Commented Sep 3, 2015 at 19:39

2 Answers 2

2

Well looks like they all use methods 4 and 3

So you only need to define the methods in one class, the other classes can then extend it.

The other methods could be handled by traits but its a bit messy 'cause you'd need to have a trait per method

The same applies to the properties.

class class1 { 
  use Method1,Method2;
  public method method3($x) {echo('hey '.$x;}
  public method method4($x) {echo('seeya '.$x;}
}

class class2 extends class1{ 
  use Method2,Method5;
}

class class3 extends class1{ 
  use Method1,Method5;      
}

trait Method1 {
  public method method1($x) {echo('hello '.$x;}
} 

trait Method2 {
  public method method2($x) {echo('goodby '.$x;}
} 

trait Method5 {
  public method method5($x) {echo('latter '.$x;}
} 

EDIT

I just realized that all the classes would end up with a method 1 and 2 as they extend class1 which uses them, so.. I guess you'd be better to have all the methods in separate traits and add them with use as required and not bother with extends

I still don't much care for it though

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Andrew, I somewhat came up with a similar approach (see my proposed answer). Not sure what I think about it either.
The code you posted in the edit looks good. As useful as traits are, they don't play too well with netbeans, it does tend to break the intellisense / code usage detection unfortunately. I'd be interested to know how it plays with your IDE and if well, which one you use
I've never yet used netbeans. I use NuShere's PHPed. The code I showed was just made on a text editor, so I am not sure what PHPed will think of it.
2

Untested and not positive it will work. Also, concerned it will be difficult to maintain.

trait trait_1{
  var $prop1=123;
  public method method1($x) {echo('hello '.$x;}
}
trait trait_2{
  $prop2=array();
  public method method2($x) {echo('goodby '.$x;}
}
trait trait_5{
   $prop5=555;
  public method method5($x) {echo('latter '.$x;}
}

trait trait_3_4{
  var $prop3, $prop4;
  public method method3($x) {echo('hey '.$x;}
  public method method4($x) {echo('seeya '.$x;}
}

class class1 { 
  use trait_1, trait_2,  trait_3_4;
}
class class2 { 
  use trait_2,  trait_3_4, trait_5;
}
class class3 { 
  use trait_1,  trait_3_4, trait_5;
}

Comments

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.