AquariaXI Posted May 19, 2021 Share Posted May 19, 2021 (edited) Hi, I'm new here! So I'm trying to create a class & I need to grab the title of my program through `get_appTitle ( )` whose data is changed by `__InitializeData ( )` How can I do that? <?php class MainframeData { public $appTitle; public $appName = "App Name"; public $version = "1.0"; public $verPrefix = ""; function __construct ( ...$params ) { $this -> param = $params; } function get_appTitle ( ) { return $this -> appTitle; } function __InitializeData ( ) { if ( $this -> verPrefix !== "" && empty ( $this -> verPrefix ) === false ) { $this -> appVer = ( $this -> version . " { " . $this -> verPrefix . " } " ); $this -> appTitle = $appName . " - " . "Version" . " " . $appVer; } if ( $this -> verPrefix !== "" || empty ( $this -> verPrefix ) === true ) { $this -> appVer = $this -> version; $this -> appTitle = $this -> appName . " - " . " { " . "Version" . " " . $this -> appVer . " } "; } } } $mfrdata = new MainframeData ( ); $mfrdata -> __InitializeData ( ); ?> Any help is GREATLY appreciated! Edited May 19, 2021 by AquariaXI Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/ Share on other sites More sharing options...
requinix Posted May 19, 2021 Share Posted May 19, 2021 Have you tried calling that get_appTitle() method? Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586661 Share on other sites More sharing options...
AquariaXI Posted May 19, 2021 Author Share Posted May 19, 2021 Yes & it returns nothing at all. Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586662 Share on other sites More sharing options...
requinix Posted May 19, 2021 Share Posted May 19, 2021 What's the code where you call it? Is it part of the code you posted before or is it written somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586664 Share on other sites More sharing options...
AquariaXI Posted May 19, 2021 Author Share Posted May 19, 2021 (edited) It is written on index.php which is separate but here it is : <?php $mfrdata = new MainframeData ( ); ?> <h1><?php echo "AppTitle :: " . $mfrdata -> get_appTitle ( ); ?></h1><br> <hr><br> Edited May 19, 2021 by AquariaXI Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586665 Share on other sites More sharing options...
requinix Posted May 19, 2021 Share Posted May 19, 2021 You already know that the appTitle is set by InitializeData. If you don't call InitializeData then there will be no appTitle. All that InitializeData code should exist in the constructor anyways. Move it and you won't have to call any special methods before you can use appTitle. Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586667 Share on other sites More sharing options...
AquariaXI Posted May 19, 2021 Author Share Posted May 19, 2021 @requinix: I don't understand... Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586669 Share on other sites More sharing options...
requinix Posted May 19, 2021 Share Posted May 19, 2021 Did you write the code for the MainframeData class? If not, where did it come from? Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586670 Share on other sites More sharing options...
AquariaXI Posted May 20, 2021 Author Share Posted May 20, 2021 yes i wrote the code for the mainframedata class. I'm just not understanding what & / or how you're trying to tell me what to do. Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586674 Share on other sites More sharing options...
Solution requinix Posted May 20, 2021 Solution Share Posted May 20, 2021 All that code in InitializeData should be inside the constructor instead. There is no need for InitializeData to exist. It forces you to call that method every time you create a new instance of MainframeData, and if you forget then you get problems like the one that started this thread. If you need to set up appVer and appTitle when the object is created then you should be literally setting up appVer and appTitle when the object is created. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586676 Share on other sites More sharing options...
AquariaXI Posted May 20, 2021 Author Share Posted May 20, 2021 Ah thanks, @requinix! That fixed it! Quote Link to comment https://forums.phpfreaks.com/topic/312742-need-help-with-class/#findComment-1586679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.