1

I'm trying to export some classes into a library to keep my code tidy but I just don't know why this doesnt seem to work :( the include file is definitely being loaded. when I defined the class locally there was also no problem and testgerät was sent.

I always try to keep things simple at first so I'm using this small sample: Gerät.php:

<?php
echo "loading Gerät<br>";
class Gerät{
    function Gerät(){
        $this->name = "testgerät";
    }
}
?>

index.php

<?php
include "/var/www/html/aiberry/objects/Gerät.php";

echo "My first PHP script!<br>";


$test = new Gerät();

echo $test->name;
?>

the output of index.php

loading Ger�t
My first PHP script!

could this be a namespace issue?

EDIT: I'm sorry for causing confusion regarding the actual issue: I am in fact expecting the output:

loading Ger�t
My first PHP script!
testgerät

If I knew how to turn the debugs on I would probably get a message telling me that there is no class "Gerät". The paradox for me though is that Gerät.php is definitely being loaded because the output "loading Gerät" is written within that file. a simple copy paste of the class into index.php delivers the desired output so i can only think of a name space issue or something alike where the class is just being lost after Gerät.php is done processing.

Andreas
  • 51
  • 5
  • What does "this doesnt seem to work" actually mean? What _exactly_ is your issue? – arkascha Oct 16 '16 at 20:54
  • You **shouldn't** use exotic character in classnames and variables and everything else that isn't a `string`. value in PHP. – gskema Oct 16 '16 at 20:55
  • @gskema There is no such thing as an "exotic character" or a "special character". There are only characters, lots of 'em buggers. Actually those german umlauts _are_ valid in php class names. That may cause issues though if those characters are also used in file names... – arkascha Oct 16 '16 at 20:58
  • 1
    I think, from reading it over, his issue is the last echo statement isn't printing anything. If he puts the class in the same file then it prints. – Dave Thomas Oct 16 '16 at 20:58
  • Try on the command line: `$ file Gerät.php` and `$ file index.php` and show the output. – Progrock Oct 16 '16 at 21:11
  • Have you declared any namespaces within either of those files? – Progrock Oct 16 '16 at 21:13
  • How are you running your scripts? – Progrock Oct 16 '16 at 21:15
  • 1
    *"could this be a namespace issue?"* - No, it's an encoding issue. – Funk Forty Niner Oct 16 '16 at 21:56
  • Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Funk Forty Niner Oct 16 '16 at 21:57
  • Plus, not a good idea using the same names for classes and functions. – Funk Forty Niner Oct 16 '16 at 21:59
  • @Fred-ii- Probably a tutorial back from php-4 times... – arkascha Oct 16 '16 at 22:03
  • @arkascha *Prolly*. The only time I go back in time, is when I watch past episodes of *"Back to the Future"*. – Funk Forty Niner Oct 16 '16 at 22:04
  • Not sure what the issue is now. I ran it on the command line using php 5.5. I got this output: loading Gerät
    My first PHP script!
    testgerät. Only difference is I changed the include path to be relative to index.php for me. https://github.com/davethomas11/so40075295 . Original poster when you return please add more information to this question, outlining what the issue is. Also if anyone reading this is running Ubuntu 16 please run this and post your findings.
    – Dave Thomas Oct 16 '16 at 22:25
  • it was indeed an encoding problem. for some reason my includes were all ANSI while index.php is encoded in utf-8... ty for the help and advice – Andreas Oct 19 '16 at 11:39
  • Exactly as I said. I had a flame war with some random dude saying it could be another reason. As I explained in my answer's comments (which have been deleted by some mod) there was no other explanation given your code and your sample output. – Johnny Oct 19 '16 at 12:52

1 Answers1

1

My guess is that you are having different charset encodings between the two files, can you verify with your text editor if Gerät.php was saved with the same character set encoding of index.php?

In general it's a good idea to avoid non-ascii character in PHP identifiers (class names, variable names, etc)

Johnny
  • 1,770
  • 12
  • 16