0

I am developing an app where I need to log the proccess. So I was loggin from main.php and now I need to log from another class (class_TestingLog.php)

Next code is how I am trying. Could you tell me what I am doing wrong?

Thank you in advance.

main.php

[...]

#Logging class
include_once("./classes/class_log.php"); 
$Class_Log = New Class_Log();

#TestingLog Class
include_once("./classes/class_testingLog.php");
$Class_TestingLog = New Class_TestingLog(); 

[...]

$Class_Log->FreeLog("Test log");
$Class_TestingLog->AnyFuncion();`

[...]

class_log.php

[...]

public function FreeLog($text)
{
    // echo $text . "\n"; #mistake
    $this->outputText .= text; #correct one
}

[...]

class_testingLog.php

private $Class_Log = '';

[...]

public function __construct()
{
    #Requires
    require_once("./classes/class_log.php");

    $this->Class_Log = New Class_Log();
}

public function AnyFuncion()
{
    var_dump($this); #From real file
    $this->Class_Log->FreeLog("Test log from another classs");
}

[...]

Browser output

Test log

Browser output expected

Test log
Test log from another classs

===========================================================================

EDIT

I made an error copying FreeLog(); It stores the parameter string value into a private variable instead echo the variable.

6
  • 1
    the code above, in isolation, works - see eval.in/882009. Are you getting any errors or warnings as well? Perhaps a include did not work or something? Have you got PHP error reporting enabled? Commented Oct 18, 2017 at 8:59
  • No, any warning or error. Got this at the first lines of main.php: error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Oct 18, 2017 at 9:07
  • Why not just extend your class_testingLog to class_log? instead of calling it again which is redundant Commented Oct 18, 2017 at 9:07
  • I will test @hungrykoala, I am relatively new in PHP Classes. Did not worked yet with extend. Commented Oct 18, 2017 at 9:09
  • Read about inheritance. It's one of the features of OOP that should be used often to reduce redundant codes. Commented Oct 18, 2017 at 9:29

1 Answer 1

1

You require_once statement inside __construct for Class_TestingLog is invalid and unnecessary. As both files are in the same directory it should be "class_log.php" not "./classes/class_log.php". There is no need for it anyway, as when you include them both in main.php it is loaded already. So try it without the require_once.

EDIT: As discussed in chat do it like this:

in main.php:

$Class_TestingLog = New Class_TestingLog($Class_Log); 

in class_testingLog.php:

public function __construct($ClassLog)
{
    $this->Class_Log = $ClassLog;
}

This will use the same instance inside the Class_TestingLog class as on the main.php.

Sign up to request clarification or add additional context in comments.

9 Comments

Tested it: Warning: require_once(./class_log.php): failed to open stream: No such file or directory in... No warning with ./classes/class_log.php
@JordiHuertas fixed it. remove ./ also, there is no need to use require_once anyway.
Without require_once do the same like the example post. It is not loggin anything.
@JordiHuertas It is working on my localhost, the problem should be somewhere else then.
Thank you, I will test from the beggining. Really the script is already very big, it has cost me quite reduce it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.