Jump to content


Photo

PHP OOP Question


  • Please log in to reply
3 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 11 May 2006 - 09:03 AM

<?
include("includes/languageClass.php");
    $langObject = new language();

function parse($langDir)
    {
        $langObject->title = getValueFromLabel($langDir, "text.lang", "title");
        $langObject->language = getValueFromLabel($langDir, "text.lang", "language");
        $langObject->countryName = getValueFromLabel($langDir, "text.lang", "countryName");
        $langObject->mainParagraph = getValueFromLabel($langDir, "text.lang", "mainParagraph");
        $langObject->imageFlag = getValueFromLabel($langDir, "references.lang", "imageFlag");
        $langObject->imageSymbol = getValueFromLabel($langDir, "references.lang", "imageSymbol");
    }
?>    
SOME HTML
<?php
    
    echo "TITLE = " . $langObject->title . "<br>";
    echo "LANGUAGE = " . $langObject->language . "<br>";
    echo "COUNTRY NAME = " . $langObject->countryName . "<br>";
    echo "MAIN PARAGRAPH = " . $langObject->mainParagraph . "<br>";
    echo "IMAGE FLAG = " . $langObject->imageFlag . "<br>";
    echo "IMAGE SYMBOL = " . $langObject->imageSymbol . "<br>";
    
?>

Don't worry about getValueFromLabel() all it does is return a string. Nothng is echoed when I echo any of the $langObject class member vars. Any ideas why?

Thanks!

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 May 2006 - 11:35 AM

[!--quoteo(post=373230:date=May 11 2006, 07:03 PM:name=JustinK101)--][div class=\'quotetop\']QUOTE(JustinK101 @ May 11 2006, 07:03 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?
include("includes/languageClass.php");
    $langObject = new language();

function parse($langDir)
    {
        $langObject->title = getValueFromLabel($langDir, "text.lang", "title");
        $langObject->language = getValueFromLabel($langDir, "text.lang", "language");
        $langObject->countryName = getValueFromLabel($langDir, "text.lang", "countryName");
        $langObject->mainParagraph = getValueFromLabel($langDir, "text.lang", "mainParagraph");
        $langObject->imageFlag = getValueFromLabel($langDir, "references.lang", "imageFlag");
        $langObject->imageSymbol = getValueFromLabel($langDir, "references.lang", "imageSymbol");
    }
?>    
SOME HTML
<?php
    
    echo "TITLE = " . $langObject->title . "<br>";
    echo "LANGUAGE = " . $langObject->language . "<br>";
    echo "COUNTRY NAME = " . $langObject->countryName . "<br>";
    echo "MAIN PARAGRAPH = " . $langObject->mainParagraph . "<br>";
    echo "IMAGE FLAG = " . $langObject->imageFlag . "<br>";
    echo "IMAGE SYMBOL = " . $langObject->imageSymbol . "<br>";
    
?>

Don't worry about getValueFromLabel() all it does is return a string. Nothng is echoed when I echo any of the $langObject class member vars. Any ideas why?

Thanks!
[/quote]

Your parse function does not have access to $langObject.
you need to either global it or pass it into function as a param.

to use global:

function parse($langDir)
{
global $langObject;

// rest of the code
...

}

i suppose you know to pass it as a param.

Life's too short for arguing.

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 12 May 2006 - 09:29 PM

HVLE:

Thank you much. That seems stupid you have to set it global in the scope of the function itself. I was trying to the make the instance where I create it global but I kept on getting parse errors. I was trying to do:

global $languageObject = new language();


#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 13 May 2006 - 01:08 AM

[!--quoteo(post=373324:date=May 13 2006, 07:29 AM:name=JustinK101)--][div class=\'quotetop\']QUOTE(JustinK101 @ May 13 2006, 07:29 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
HVLE:

Thank you much. That seems stupid you have to set it global in the scope of the function itself. I was trying to the make the instance where I create it global but I kept on getting parse errors. I was trying to do:

global $languageObject = new language();
[/quote]


Yeah, I agree. I can't think of any good reason why they did it like that.
You'll find a lot more of "stupid" differences while working with PHP syntax. (I.E. passing by reference, private, public in class). PHP is not a good OOP language.
Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users