Jump to content
Sign in to follow this  
osherdo

some object oriented problem

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?

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.