9 Replies - 1303 Views - Last Post: 11 October 2012 - 10:10 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Calling class from another file

Posted 10 October 2012 - 12:55 PM

Hi!

I'm getting insane with this.
I've the following:

login.php
class MyClass
{
    public $var = "I'm a class property!";
}

main.php
if (isset($_POST['submit'])) {
			include('login.php');
			$obj = new MyClass;  
			echo $obj->var();  
		}

My question is: Why this doesn't work? It doesn't appear anything on the screen.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling class from another file

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3493
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Calling class from another file

Posted 10 October 2012 - 12:58 PM

then you have disabled error display. you try to call a method (which doesn’t exist and therefore produces an error) instead of a property.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,036
  • Joined: 08-August 08

Re: Calling class from another file

Posted 10 October 2012 - 01:17 PM

Try echo $obj->var;

This post has been edited by CTphpnwb: 10 October 2012 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Calling class from another file

Posted 10 October 2012 - 01:54 PM

View PostDormilich, on 10 October 2012 - 12:58 PM, said:

then you have disabled error display. you try to call a method (which doesn’t exist and therefore produces an error) instead of a property.

...?

Doesn't work either.
Was This Post Helpful? -1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3493
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Calling class from another file

Posted 10 October 2012 - 10:59 PM

what do you mean? an explanation certainly does not make faulty code correct. it gives you an idea what went wrong and therefore where to fix things.
Was This Post Helpful? 2
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3493
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Calling class from another file

Posted 10 October 2012 - 11:11 PM

besides that: "It doesn’t work" is not good enough

This post has been edited by Dormilich: 10 October 2012 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Calling class from another file

Posted 10 October 2012 - 11:32 PM

View PostDormilich, on 10 October 2012 - 07:58 PM, said:

then you have disabled error display.

Just to add a bit of an explanation to this.

PHP has a few configuration options that allow you to log and/or display error messages. During development you should always display all errors so that they won't go unnoticed and can be dealt with, whereas when your code is moved on to production servers you should turn the error display off and log them instead. You don't want normal users to have to see these errors.

To turn error reporting on on your development machine, set the display_errors directive to "On", and the error_reporting directive to "E_ALL".

If you can't change those in the PHP config file, you can turn them on for individual scripts by adding this to the very top:
ini_set("display_errors", true);
error_reporting(E_ALL);


Note, however, that errors that prevent the script from running at all, like syntax errors, will still not show if you do it like this. It's better to set them in the PHP config.
Was This Post Helpful? 2
  • +
  • -

#8 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Calling class from another file

Posted 11 October 2012 - 09:54 AM

View PostDormilich, on 10 October 2012 - 11:11 PM, said:


As I said:

View PostKeylogger, on 10 October 2012 - 12:55 PM, said:

(...)It doesn't appear anything on the screen.

I can't be more specific.


View PostAtli, on 10 October 2012 - 11:32 PM, said:

View PostDormilich, on 10 October 2012 - 07:58 PM, said:

then you have disabled error display.

ini_set("display_errors", true);
error_reporting(E_ALL);


Doens't seem to work..because I do not get any kind of error, it doesn't display anything.
Was This Post Helpful? 0
  • +
  • -

#9 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Calling class from another file

Posted 11 October 2012 - 10:03 AM

The problem is on this line:
if(isset($_POST['submit'])) {

}

Because, if I put only:
include('login.php');
$obj = new MyClass;  
echo $obj->prop1; 

It works.
Was This Post Helpful? 0
  • +
  • -

#10 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 14-February 11

Re: Calling class from another file

Posted 11 October 2012 - 10:10 AM

Okey..solved.
Changed this:
<form action="login.php" method="post">

to this:
<form action="" method="post">


Thank you all. :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1