Re: [PHP] simple class constructor
Can you paste the index page's code here? If the page is going blank there's probably an error (syntax, bad file path, etc). If you have access you can turn error reporting on so you can actually see the error - or better yet check the php error log file. Settings for both of these are in the php.ini file, though sometimes they can be overridden by apache directives (depending on the setup). http://php.net/manual/en/ini.core.php Chris. On Tue, Oct 19, 2010 at 4:12 PM, David McGlone da...@dmcentral.net wrote: Hi everyone, I've been really good at googling to find my answers, but this time my method isn't working for me. I have created a very simple class and a constructor hoping to get a much better understanding of how to work with them. The code works, but because it's very simple, I'm not sure that the way I'm trying to access it is possible. All I'm trying to do is access the class from outside the function. (is that how to describe it?) For instance I have this simple code: class simpleConstructer { function __construct() { echo running the constructor; } } $test=new simpleConstructer(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. The thought of a session crossed my mind, but I'm pretty confident at my level to know I don't need a session. Could someone point me in the right direction or give me some pointers, advice? -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
Also wanted to point out that you can check the error reporting level and log file location (really all of the php's settings) by calling phpinfo(); in your code. ?php phpinfo(); ? Chris. On Tue, Oct 19, 2010 at 4:54 PM, chris h chris...@gmail.com wrote: Can you paste the index page's code here? If the page is going blank there's probably an error (syntax, bad file path, etc). If you have access you can turn error reporting on so you can actually see the error - or better yet check the php error log file. Settings for both of these are in the php.ini file, though sometimes they can be overridden by apache directives (depending on the setup). http://php.net/manual/en/ini.core.php Chris. On Tue, Oct 19, 2010 at 4:12 PM, David McGlone da...@dmcentral.netwrote: Hi everyone, I've been really good at googling to find my answers, but this time my method isn't working for me. I have created a very simple class and a constructor hoping to get a much better understanding of how to work with them. The code works, but because it's very simple, I'm not sure that the way I'm trying to access it is possible. All I'm trying to do is access the class from outside the function. (is that how to describe it?) For instance I have this simple code: class simpleConstructer { function __construct() { echo running the constructor; } } $test=new simpleConstructer(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. The thought of a session crossed my mind, but I'm pretty confident at my level to know I don't need a session. Could someone point me in the right direction or give me some pointers, advice? -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote: Hi everyone, I've been really good at googling to find my answers, but this time my method isn't working for me. I have created a very simple class and a constructor hoping to get a much better understanding of how to work with them. The code works, but because it's very simple, I'm not sure that the way I'm trying to access it is possible. All I'm trying to do is access the class from outside the function. (is that how to describe it?) For instance I have this simple code: class simpleConstructer { function __construct() { echo running the constructor; } } $test=new simpleConstructer(); You're trying to instantiate the class. And the way you're doing it here is correct. When you do this, $test becomes an object of this class. If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. You've likely got an error you're not seeing. Fix this first. If the file your class is in is syntactically correct, and you do include simpleConstructerFile.php; in your index.php file, it should flawlessly include the code. Then, in your index.php, you do this: $test = new simpleConstructer; you should see the contents of the echo statement appear on the page. So you're on the right track. You just need to find the error first. The thought of a session crossed my mind, but I'm pretty confident at my level to know I don't need a session. You're right. Using a session would be completely unnecessary, and I'm not even sure how it would assist at all. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
On Tue, 2010-10-19 at 17:15 -0400, Paul M Foster wrote: On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote: snip You're trying to instantiate the class. And the way you're doing it here is correct. When you do this, $test becomes an object of this class. If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. You've likely got an error you're not seeing. Fix this first. If the file your class is in is syntactically correct, and you do include simpleConstructerFile.php; in your index.php file, it should flawlessly include the code. Then, in your index.php, you do this: $test = new simpleConstructer; you should see the contents of the echo statement appear on the page. So you're on the right track. You just need to find the error first. Ah ha! Thank you! Your mention of an error, was spot on. notice below I misspelled the class name but got the Object name correct. Also at first I had the setup like this because it wasn't working and I thought I was doing it wrong: (this also added to my confusion) myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } index.php require_once 'myclass.php'; $test = new simpleConstructor(); But once I fixed the error I put it all back in myclass.php like so: myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); Now I am wondering what you meant when you said: If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); If you don't mind my asking, how would you take the above example and change it to what you describe above? -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] simple class constructor
-Original Message- From: David McGlone [mailto:da...@dmcentral.net] Sent: Tuesday, October 19, 2010 4:32 PM To: php-general@lists.php.net Subject: Re: [PHP] simple class constructor On Tue, 2010-10-19 at 17:15 -0400, Paul M Foster wrote: On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote: snip You're trying to instantiate the class. And the way you're doing it here is correct. When you do this, $test becomes an object of this class. If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. You've likely got an error you're not seeing. Fix this first. If the file your class is in is syntactically correct, and you do include simpleConstructerFile.php; in your index.php file, it should flawlessly include the code. Then, in your index.php, you do this: $test = new simpleConstructer; you should see the contents of the echo statement appear on the page. So you're on the right track. You just need to find the error first. Ah ha! Thank you! Your mention of an error, was spot on. notice below I misspelled the class name but got the Object name correct. Also at first I had the setup like this because it wasn't working and I thought I was doing it wrong: (this also added to my confusion) myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } index.php require_once 'myclass.php'; $test = new simpleConstructor(); But once I fixed the error I put it all back in myclass.php like so: myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); Now I am wondering what you meant when you said: If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); If you don't mind my asking, how would you take the above example and change it to what you describe above? class simpleConstructer { function __construct() { echo running the constructor; } function myFunction() { echo 'this is another function/method within the class simpleConstructor'; } } $test = new simpleConstructor(); $test-myfunction(); Regards, Tommy -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
Note that you still have a typo, but maybe it's only in your email messages: class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); The class is misspelled; it should be simpleConstructor. As a side note, it's common convention to name classes with a leading capital letter, e.g. SimpleConstructor. That's just convention, though, and I'm sure it differs in some languages. Even in PHP stdClass doesn't, but most other classes do. David
RE: [PHP] simple class constructor
On Tue, 2010-10-19 at 16:53 -0700, Tommy Pham wrote: -Original Message- From: David McGlone [mailto:da...@dmcentral.net] Sent: Tuesday, October 19, 2010 4:32 PM To: php-general@lists.php.net Subject: Re: [PHP] simple class constructor On Tue, 2010-10-19 at 17:15 -0400, Paul M Foster wrote: On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote: snip You're trying to instantiate the class. And the way you're doing it here is correct. When you do this, $test becomes an object of this class. If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. You've likely got an error you're not seeing. Fix this first. If the file your class is in is syntactically correct, and you do include simpleConstructerFile.php; in your index.php file, it should flawlessly include the code. Then, in your index.php, you do this: $test = new simpleConstructer; you should see the contents of the echo statement appear on the page. So you're on the right track. You just need to find the error first. Ah ha! Thank you! Your mention of an error, was spot on. notice below I misspelled the class name but got the Object name correct. Also at first I had the setup like this because it wasn't working and I thought I was doing it wrong: (this also added to my confusion) myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } index.php require_once 'myclass.php'; $test = new simpleConstructor(); But once I fixed the error I put it all back in myclass.php like so: myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); Now I am wondering what you meant when you said: If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); If you don't mind my asking, how would you take the above example and change it to what you describe above? class simpleConstructer { function __construct() { echo running the constructor; } function myFunction() { echo 'this is another function/method within the class simpleConstructor'; } } $test = new simpleConstructor(); $test-myfunction(); Thank you Tommy. Now it all comes together and I believe I understand now. Does the code immediately after the __construct automatically run, but when adding more methods to the class, they need to be called with the $name-Object_name? Is my thinking correct? -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
On Tue, 2010-10-19 at 17:05 -0700, David Harkness wrote: Note that you still have a typo, but maybe it's only in your email messages: class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); The class is misspelled; it should be simpleConstructor. As a side note, it's common convention to name classes with a leading capital letter, e.g. SimpleConstructor. That's just convention, though, and I'm sure it differs in some languages. Even in PHP stdClass doesn't, but most other classes do. Thank you David, the typo was in my code. :-/ As for the class names, I agree with you. I've read so many books where things are changed up that I can't remember which way to do it. In this case since I was playing around for learning purposes, I just guessed and run with it. I appreciate the heads up :-) -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
The constructor is the __construct() method, and it gets executed automatically when you instantiate the class into an object. The class defines the state (fields/properties) and behavior (methods/functions) that its objects will have. Instantiating the class is the fancy term for creating a new object with that state and behavior and calling the class's constructor on it. From then on you can call other methods on the object and access its public state. David
RE: [PHP] simple class constructor
-Original Message- From: David McGlone [mailto:da...@dmcentral.net] Sent: Tuesday, October 19, 2010 5:32 PM To: php-general@lists.php.net Subject: RE: [PHP] simple class constructor On Tue, 2010-10-19 at 16:53 -0700, Tommy Pham wrote: -Original Message- From: David McGlone [mailto:da...@dmcentral.net] Sent: Tuesday, October 19, 2010 4:32 PM To: php-general@lists.php.net Subject: Re: [PHP] simple class constructor On Tue, 2010-10-19 at 17:15 -0400, Paul M Foster wrote: On Tue, Oct 19, 2010 at 04:12:51PM -0400, David McGlone wrote: snip You're trying to instantiate the class. And the way you're doing it here is correct. When you do this, $test becomes an object of this class. If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); Basically I want to learn how I can (if it's possible with this simple code) is display the output on a different page. I tried putting the line: $test=new simpleConstructer(); on the index page and including the page the class is on, but it causes the index page to go blank. You've likely got an error you're not seeing. Fix this first. If the file your class is in is syntactically correct, and you do include simpleConstructerFile.php; in your index.php file, it should flawlessly include the code. Then, in your index.php, you do this: $test = new simpleConstructer; you should see the contents of the echo statement appear on the page. So you're on the right track. You just need to find the error first. Ah ha! Thank you! Your mention of an error, was spot on. notice below I misspelled the class name but got the Object name correct. Also at first I had the setup like this because it wasn't working and I thought I was doing it wrong: (this also added to my confusion) myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } index.php require_once 'myclass.php'; $test = new simpleConstructor(); But once I fixed the error I put it all back in myclass.php like so: myclass.php class simpleConstructer { function __construct() { echo running the constructor; } } $test = new simpleConstructor(); Now I am wondering what you meant when you said: If you had another function (member) within the class called myfunction(), you could run it this way (after you instantiate the class): $test-myfunction(); If you don't mind my asking, how would you take the above example and change it to what you describe above? class simpleConstructer { function __construct() { echo running the constructor; } function myFunction() { echo 'this is another function/method within the class simpleConstructor'; } } $test = new simpleConstructor(); $test-myfunction(); Thank you Tommy. Now it all comes together and I believe I understand now. Does the code immediately after the __construct automatically run, but when adding more methods to the class, they need to be called with the $name-Object_name? Is my thinking correct? -- Blessings David M. I had a misspell there due to copy and paste :)) ... Anyway, when you instantiate the class, the __construct() is executed. What you specified inside that __construct() will run automatically when instantiate (create the class object). Example: class MyClass() { function __construct() { $this-init(); } function init() { // init your class for whatever you want to do } function executeTaskOne() { // to do one task } function executeTaskTwo() { // to do another task } } There's no limit on how many methods you can have for the class but it comes down to overall application design for the purpose needed. There's also something called visibility too. You might want to check [1] for indepth explaination and samples. Regards, Tommy [1] http://www.php.net/manual/en/language.oop5.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple class constructor
On Tue, 2010-10-19 at 17:41 -0700, David Harkness wrote: The constructor is the __construct() method, and it gets executed automatically when you instantiate the class into an object. The class defines the state (fields/properties) and behavior (methods/functions) that its objects will have. Instantiating the class is the fancy term for creating a new object with that state and behavior and calling the class's constructor on it. From then on you can call other methods on the object and access its public state. Ye! Ha! Just as I suspected! I can now say I have a very thorough understanding of Classes, Objects and methods. :-) -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] simple class constructor
[snip] Ye! Ha! Just as I suspected! I can now say I have a very thorough understanding of Classes, Objects and methods. :-) [/snip] May I suggest Head First OOP? They don't do PHP in it but it is very valuable for learning about things like encapsulation and some other cool words. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] simple class constructor
On Tue, 2010-10-19 at 20:25 -0500, Jay Blanchard wrote: [snip] Ye! Ha! Just as I suspected! I can now say I have a very thorough understanding of Classes, Objects and methods. :-) [/snip] May I suggest Head First OOP? They don't do PHP in it but it is very valuable for learning about things like encapsulation and some other cool words. You sure can :-) I'm open to anything that I can use to make me better at programming. I'll check it out on amazon and maybe add it to my wishlist. IIRC there was a discussion about PHP books a while back. I'm also gonna see if I can dig that thread up. I was at half price books today looking for a good book on PHP to add to my collection, because the ones I have a quickly becoming outdated but I didn't find anything. Maybe better luck next time. I am reluctant to buy books off the internet, because I'm afraid when I receive them, they aren't actually any good and they become a waste of my money. -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] simple class constructor
On Tue, 2010-10-19 at 20:25 -0500, Jay Blanchard wrote: [snip] Ye! Ha! Just as I suspected! I can now say I have a very thorough understanding of Classes, Objects and methods. :-) [/snip] May I suggest Head First OOP? They don't do PHP in it but it is very valuable for learning about things like encapsulation and some other cool words. You sure can :-) I'm open to anything that I can use to make me better at programming. I'll check it out on amazon and maybe add it to my wishlist. IIRC there was a discussion about PHP books a while back. I'm also gonna see if I can dig that thread up. I was at half price books today looking for a good book on PHP to add to my collection, because the ones I have a quickly becoming outdated but I didn't find anything. Maybe better luck next time. I am reluctant to buy books off the internet, because I'm afraid when I receive them, they aren't actually any good and they become a waste of my money. -- Blessings David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php