osherdo Posted January 27, 2016 Share Posted January 27, 2016 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted January 28, 2016 Share Posted January 28, 2016 The code you have posted executes just fine: https://3v4l.org/VgRDX Quote Link to comment Share on other sites More sharing options...
osherdo Posted January 28, 2016 Author Share Posted January 28, 2016 then why it shows an error in my machine? Quote Link to comment Share on other sites More sharing options...
requinix Posted January 28, 2016 Share Posted January 28, 2016 Because the code on your machine is not the exact same code you posted here. Even more so because FatalErrorException is not a built-in class to PHP, which means there is some other code somewhere that defines and throws it. Quote Link to comment Share on other sites More sharing options...
osherdo Posted January 28, 2016 Author Share Posted January 28, 2016 I am actually using Laravel 5.1. you think it would be better to ask in a laravel's forum? I did posted partial code. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted January 28, 2016 Share Posted January 28, 2016 They cannot help you either until you post the actual, full code. The error message indicates that, somewhere outside of the above code snippet, you try to call getProperty() on the class2 instance. But class2 only has a getProperty2() method, so PHP blows up, appearently delegating the exception to Laravel. Quote Link to comment Share on other sites More sharing options...
osherdo Posted January 28, 2016 Author Share Posted January 28, 2016 Please here's the entire code for you to review: https://3v4l.org/MSWq6 would like to hear your opinion. why cannot I initiate the second class ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted January 28, 2016 Share Posted January 28, 2016 You can instantiate the class. Look at the line number: The problem happens after that, namely when you try to call getProperty() on the class2 instance $obj (as I already assumed). And like I already said, class2 has no getProperty() method. It only has getProperty2(). Quote Link to comment Share on other sites More sharing options...
osherdo Posted January 28, 2016 Author Share Posted January 28, 2016 Okay I think I almost got it. Now it shows a warning in the output. can you tell me what's wrong here? https://3v4l.org/k390G Thanks. Quote Link to comment Share on other sites More sharing options...
boompa Posted January 28, 2016 Share Posted January 28, 2016 You need to learn how to read errors and warnings, as these are perfectly clear: Warning: getProperty2() expects exactly 1 parameter, 0 given in /in/k390G on line 33 Why is the getProperty2 method setup to take an argument? It looks like you copy/pasted the function for setProperty2() and forgot to remove the argument. 1 Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted January 28, 2016 Solution Share Posted January 28, 2016 Try to slow down and write your code more cleanly. This includes proper formatting. When you rush it, you'll spend most of your time debugging errors (or waiting for others to debug them for you), which is somewhat frustrating. An IDE (integrated development environment) like Netbeans or Eclipse can help you write good code, because it will notify immediately when there's an obvious problem (like a parameter which isn't used anywhere). 1 Quote Link to comment Share on other sites More sharing options...
osherdo Posted January 30, 2016 Author Share Posted January 30, 2016 Got it figured out eventually. thanks for your honest opinions. https://3v4l.org/JFvt7 - working example. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.