Search the Community
Showing results for tags 'object-oriented'.
-
Consider this code: <?php class MyClass { public $prop1 = "I'm a class property!"; public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br/>"; } } $obj = new MyClass;// Class instantiation. //var_dump($obj); echo "1st drill:"."<br>"; echo $obj->getProperty(); //echo prop1 string (// Get the property value). $obj->setProperty('I am a new property value'."<br><br>"); echo $obj->getProperty(); // Read it out again to show the change. //Second Drill echo "second drill: "."<br>"; class class2 { public $obj2="I am the first variable from the second drill."; public function setProperty2($newval2) { $this->obj2=$newval2; } public function getProperty2($newval2) { return $this->obj2."<br>"; } } // Create two objects $obj = new class2; $obj2 = new class2; When trying to create two objects in the second drill (the last 2 rows) - it returns an error: FatalErrorException in oop.php line 41: Call to undefined method class2::getProperty() why can't I initiate the class?
-
I currently have 3 big long PHP functions that correspond to 3 different product lines for which code is written for. Each function outputs certain sections of a PDF file for each of the 3 lines. A product line can have things like (Specs, Engine, Assembly, Job, etc). Each secion has slightly different computation to be done, and some product lines share certain secions, but not the others. i.e. all 3 have the need for the Specs section, but only 2 need Engine section, and so on. OO code says: create a general supporting class for all 3 product lines, then extend the class for each specific product line separate each section generation into its own function put them into the general class. Have extended classes take and customize what they need, probalby some more OO stuff, like separating each class into a separate file, doing some clean-up, maybe doing some dependency injection to initialize things, or the like. But I am thinking: here we have a big long function with everything for each of the product lines. Yes, there is some code duplication, and yes it is not OO, it is procedural right now, but the benefits of procedural here vs OO in this case is that I can leave these functions as-is, as they are written now - no need to rewrite code It is all right here in one file. I do not need to hunt for OO objects or methods spread out in different files if I need to change something with a product line, it is all right there! I can customize that product line as as needed. I do not see the need to often change all 3 product lines, but if I do, all the 3 functions are in the same file, I can search and modify code right there. I get the benefit of seeing all the details and all the steps for each product line in each file. There is no abstraction. It is a little tedious I confess, but it is all in one place. Why do we have OO paradigm now? Is it that in my case I do not need OO or am I missing something? I do not want to do OO just for the sake of it being OO. I am concerned that me reworking this code into OO will not really create me much benefit, but create "convert this to OO" work and the code will just sit there afterwards not really gathering much direct benefit from such conversion.