Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending PHP classes with different shared methods and properties

Tags:

oop

php

class

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?

like image 485
user1032531 Avatar asked May 13 '26 19:05

user1032531


2 Answers

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

like image 125
andrew Avatar answered May 16 '26 08:05

andrew


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;
}
like image 36
user1032531 Avatar answered May 16 '26 09:05

user1032531



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!