There is a table in the database where not all fields are filled. It is necessary to display only those in which the value is not null.

I decided this way, but the set of if, the code is not universal turned out

if($types->priceType != null) echo 'Π’ΠΈΠΏ: '.$types->priceType->name.'<br>'; if($types->course != null) echo 'Π’ΠΈΠΏ курс: '.$types->course->name.'<br>'; if($types->count_room != null) echo 'ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΊΠΎΠΌΠ½Π°Ρ‚: '.$types->count_room.'<br>'; if($types->count_meter != null) echo 'ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²: '.$types->count_meter.'<br>'; if($types->is_repair != null) echo 'Π‘ Ρ€Π΅ΠΌΠΎΠ½Ρ‚ΠΎΠΌ'.'<br>'; if($types->is_new_building_id != null) echo 'НовоС Π·Π΄Π°Π½ΠΈΠ΅: '.$types->is_new_building_id.'<br>'; if($types->count_hundredth != null) echo 'ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ соток: '.$types->count_hundredth.'<br>'; if($types->type_realty_id != null) echo 'Π’ΠΈΠΏ нСдвиТимости: '.$types->type_realty_id.'<br>'; 

Tell me, is it possible to rewrite in some way so that there are no repetitions with if ??

  • Yes, I did not immediately notice that for each field you have your own name, so I think you don’t need to do anything here and leave everything as it is, anyway, because of the names, you will need to prescribe the output with a condition in some place. Reduce the amount of code here, increase elsewhere - heff

3 answers 3

You can make a couple of methods:

1) returns the field object by name

 function returnObj($obj, $field) { return $obj->{$field}; } 

2) returns the value of the field, if you give the object an input, a field to check and an array of fields to get to. Something like this:

 function getValue2($myObj, $fieldCheck, $fieldsGet) { if ($myObj->$fieldCheck != null) { $obj = $myObj; foreach ($fieldsGet as $val) { if ($obj->{$val} != null) { $obj = returnObj($obj, $val); } } return $obj; } } 

In the end, let's say there are classes

 class Test { public $pole = 666; public $priceTypes = null; public function __construct() { $this->priceTypes = new Price(); } } class Price { public $name = 999; } 

then there will be something like this:

 $test = new Test(); echo getValue2($test, 'priceTypes', ['priceTypes', 'name']); 

or if there is no field chain, then a simple method

 function getValue($testy, $field) { if ($testy->$field != null) return $testy->{$field}; } 

and receiving:

 $test = new Test(); echo getValue($test, 'pole'); 

    Option 1

     echo $types->priceType?'Π’ΠΈΠΏ: '.$types->priceType->name.'<br>':''; echo $types->course->name?'Π’ΠΈΠΏ курс: '.$types->course->name.'<br>':''; 

    etc.

    Option 2

    Add $ types to the object class, for example, methods

     public function priceType(){ return $this->priceType?'Π’ΠΈΠΏ: '.$types->priceType->name.'<br>':''; } 

    And in the main code output

     echo $types->priceType(); 

    And such options - the car and a small truck.

    • then getPriceType ... like getter - Alexey Shimansky
    • @ Alexey Shimansky is not a getter, from the word at all. Something like getPriceTypePrintableText would probably do here, but I decided not to produce unnecessary entities. - rjhdby

    In order not to copy if, you can loop through the object using the foreach loop, php is not at hand, in my memory as follows:

      foreach ($types as $key => $value) { if ($value != null) echo "$key: ".$value.'<br>'; } 

    If I made a mistake please write, I will delete the answer.

    • You can go through it, but 1) It’s not a fact that all the properties of the object are needed, 2) The output order is important, 3) The text for each property is different, 4) Some properties are objects themselves and their properties need to be printed - $ types-> course-> name, $ types-> count_meter - rjhdby
    • I agree that the problem has not been solved so that the names for all the properties should be different, but creating a separate method for each echo, you do not decide the question, it’s better to just leave it as it is, more understandable and less code. About the rest of the requirements, where did they come from, the question asked by another user? - heff
    • The remaining requirements can be seen from the code given in the question (well, except for the order - I thought it over). With regard to individual methods, the principle of the sole responsibility has not yet been canceled, but here, nevertheless, op. - rjhdby
    • one
      The fact that OPP is needed on large projects is not in dispute (perhaps he has only 2-3 pages on the site), but I meant that by doing methods you do not answer the question of how not to write for each if property. 8 if constructions in one place = 1 if constructions in 8 places. In his example, it is better not to think of everything as it is. - heff