Jump to content

some object oriented problem


osherdo
Go to solution Solved by Jacques1,

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

  • Solution

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).

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.