Jump to content

Help writing my 1st PHP Class


TomTees

Recommended Posts

Hi everyone.  Still new to PHP and to Object-Oriented Programming.

 

My goal for today is to write a few dummy Classes in PHP that do enough so that I can see something on my webpage.

 

I am wondering if there is someone here who would be willing to help in either the forums or one-on-one.

 

It may sound like a silly request, but this is all new to me as a Procedural Programmer who has actively programmed in about 10 years!!  :-[

 

Thanks,

 

 

TomTees

 

 

Link to comment
Share on other sites

This forum is for getting help with code that you have written. In any event, it's not efficient to try and teach someone through a forum. There are plenty of tutorials out there for PHP OOP Programming. I would suggest you take a look at some. Here are a couple:

 

Beginning Object Oriented Programming in PHP

Object Oriented PHP for Beginners

 

Or there are the Tutorials on this site:

 

OO PHP Part 1: OOP in Full Effect

OO PHP Part 2: Boring OO Principles

OO PHP Part 3: UML, Classes and Relations

Link to comment
Share on other sites

I've read tons of books and was looking for some real-world help.  (If all of those books and tutorials worked I wouldn't be here now would I?)  ;)

 

I'd like some help getting answers to specific questions to take textbook examples and apply them to a practical solution.

 

Any volunteers?

 

 

TomTees

 

Link to comment
Share on other sites

I reading this thinking if you give me a proper problem I will give you proper answer,

however you could start with a database class, which would more than likely include, connection, fetch, and query methods, if you think of a problem post the code and we can then help you with it.

 

:)

 

 

Link to comment
Share on other sites

I reading this thinking if you give me a proper problem I will give you proper answer,

 

No one has given me the chance...

 

I just asked if you guys would take some time to help me get started.  I didn't say where I was lost (or why books won't help.)

 

 

TomTees

;)

 

 

Link to comment
Share on other sites

So my understanding is 1 Class should equal 1 PHP file.  (I'm trying to do this in a manly, enterprise-level way.  None of this wussy stuff in all of these books and tutorials people keep referring me too!)

 

Is that much correct?

 

I am trying to first build a "Registration Module" for my E-commerce site.

 

I think one disconnect that I'm having is how to hook Classes up to a base Webpage?!

 

Let's say I write some basic XHTML web form - which I have forgotten to do so I'll get back to you later today on that - then how do I link my Class to my Form?

 

I was thinking of having a Registration class that takes field inputs, does some checking, and creates a new Customer Record in my back-end database.

 

How do I want to approach from a Web Form to getting my Class to fire and process the form?

 

Follow my two questions?

 

 

 

TomTees

 

 

 

Link to comment
Share on other sites

I've read tons of books and was looking for some real-world help. If all of those books and tutorials worked I wouldn't be here now would I?

 

Then perhaps you should have stated that in your initial post. Otherwise it looked like you were asking for someone to personally teach you OOP.

 

OK, I'm not sure what you have been reading, but it must not have been very good. I read a couple of tutorials to get started and was able to implement OO code into my site fairly easily - although I still use a lot of procedural.

 

As for how do you "link" the class that should have been the very first thing your "books" covered. You simply include the file with the Class code as you would any other php file using one of the include()/require() variants. Then you instantiate the class by creating a new object like so:

$someVariable = new SomeClassDefiningAnObject;

Link to comment
Share on other sites

I normally think of a class a collection of functions that relate to an object in this case the web page, so you could set up your headers, title, include the css + jcript etc, in methods and just call these in the correct  order to build up a complete page

 

Should one file = one class, i guess that would make sense, if making a user object you would include the user file that holds the user class

 

 

to make a class -->>

 

<?php 

class hello
{

function hello($name)
{
	echo 'hello '.$name;
}

function error_check($var)
{
	if($var ==10) 
	{
		return true;
	}
	else return false;
}

}

$a = new hello();

         #call write stuff method
         $a->hello("PHP Freaks");
         
        #call check stuff method
        #were saying if we gat a false value echo stuff
if(!$a->error_check(1000))
		echo 'was false';
               ## or use a query method to maybe put some stuff in a Db


?>

 

apologies if my answer was crap, but you just include the class and call the methods in appropriate logical order for your page / application

 

:)

Link to comment
Share on other sites

Off topic @ sinista:

 

You never need to create an IF/ELSE statement were the IF condition returns true and the ELSE condition returns false. Instead of this

function error_check($var)
{
    if($var ==10)
    {
        return true;
    }
    else return false;
}

 

You should have just done this

function error_check($var)
{
    return ($var==10);
}

Link to comment
Share on other sites

OK, I'm not sure what you have been reading, but it must not have been very good.

 

My point is that "Go read the manual" never helped anyone do anything of value.

 

The way I - and I think most people - learn is by working on real world examples that relate to you.

 

If you wanted to go from New York to California, you'd expect someone to give you detailed direction of how to do that.  Telling someone to go buy and atlas and find the answer is silly.

 

(Not picking on you specifically, but ya follow me?!)  ;)

 

I spent a year reading books, became unemployed for a long period, and now that I'm picking up on my website I have forgotten EVERYTHING.

 

Why, because pedantic teaching styles never help you retain info.

 

So I was just trying to say, "Hey, can someone work closely with me to apply what I do know to my website?!"

 

I'm sure that once I get back up to speed, then I can go back to all of those books I bought and work primarily alone.

 

But I need some serious hand-holding right now.

 

So mentoring and coddling is welcome!!  :D

 

 

TomTees

 

P.S.  I wrote my first program back in the early 1980's, so I probably know a *few* things about computers and programming!  I just happen to be damn rusty and distracted by being unemployed and looking for work!

 

Link to comment
Share on other sites

I guess start at the beginning then,

 

so you want to make a web page, do you know HTMl, & CSS? to be able to structure a static page?

(sorry if this question is dumb, but do you have php, mysql on your PC i use a program called easyPhp to act as my localhost)

 

:)

Link to comment
Share on other sites

Your best bet is to show us some code you've written and where your concerns lie in relation to that code.  It's hard to understand a problem, let alone solve it, without seeing what you're actually doing.

 

So, post code, even if it's just pseudo-code, and chances are you'll get some real help.

Link to comment
Share on other sites

I guess start at the beginning then,

 

so you want to make a web page, do you know HTMl, & CSS? to be able to structure a static page?

 

** Sorry to clutter the forum up with my personal issues!  **  :D

 

Yes, I used to know HTML and CSS pretty good, but I have forgotten most things.

 

So I am going to be *good* and try to read a tutorial on creating a Web-Form using XHTML and CSS right now.  (Let's hope I can whip through this before you all leave for the weekend?!)

 

 

(sorry if this question is dumb, but do you have php, mysql on your PC i use a program called easyPhp to act as my localhost)

 

I have a MacBook.

I am setting up a brand new directory to work in.

I use MAMP and NetBeans for development.

Just got a test index.php file working in NetBeans, so my re-config of directories must have succeeded?!

 

I will try to throw together a Web-Form in the next hour or two...  (Been busy with job recruiters and interviews since 7:30 a.m. which i a GOOD thing!)

 

Don't leave, because I need your help!!  :)

 

 

 

TomTees

 

 

Link to comment
Share on other sites

OK, I'm not sure what you have been reading, but it must not have been very good.

 

My point is that "Go read the manual" never helped anyone do anything of value.

 

Really? Have you ever read the PHP manual? I learn a ton from it. Even when I think I know everything about a function, if I read the PHP manual for that function (especially the notes) I sometimes learn a lot of useful information.

 

But, that is besides the point because no one said to "Go read the manual". I suggested you go read a Tutorial. There is a big difference between a manual and a tutorial. A good tutorial will include a realistic problem needing to be solved and then explain how the process in question can be used to solve the problem. I read the tutorials for OOP on this site quite a while back but recall them giving good examples and context of the functionality.

 

Anyway, mnoving this post to the "Application Design" forum as it seems the most appropriate for this subject. Good luck with getting the hand-holding you need.

Link to comment
Share on other sites

To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

 

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>

Link to comment
Share on other sites

To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

 

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>

 

How should I handle my file type?

 

I think I want a .php file, right?

 

How do I intermingle the HTML and PHP?

 

To me, it is a PHP file first with the HTML just used to create the form.

 

Any thoughts?

 

 

 

TomTees

 

 

Link to comment
Share on other sites

To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

 

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>

 

How should I handle my file type?

 

I think I want a .php file, right?

 

How do I intermingle the HTML and PHP?

 

To me, it is a PHP file first with the HTML just used to create the form.

 

Any thoughts?

 

Yeah, it should be a .php file.

 

Intermixing the two is easy.  Simply use PHP tags (<?php ?>) to denote where you want to do script processing.  Example:

 

<?php echo "I'm appearing even before the HTML is rendered!"; ?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

<?php echo "And I come after everything else!"; ?>

 

Where you put the PHP for your form depends on how you want to handle form processing.  Am I right in assuming you want a sticky form?

Link to comment
Share on other sites

Yeah, it should be a .php file.

 

Intermixing the two is easy.  Simply use PHP tags (<?php ?>) to denote where you want to do script processing.  Example:

 

<?php echo "I'm appearing even before the HTML is rendered!"; ?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

<?php echo "And I come after everything else!"; ?>

 

But can you do this...

 

<?php>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

?>

<html>

 

I mean if it is a PHP file, should everything be a sub-set of the PHP tags?

 

 

Where you put the PHP for your form depends on how you want to handle form processing.  Am I right in assuming you want a sticky form?

 

Sticky will be nice later on.

 

CRAP!  I have a really good book that explains a lot of this a long ways away in storage.  (Wish I had grabbed that book last time.)

 

 

 

TomTees

 

 

Link to comment
Share on other sites

To be honest, I'm not sure what you're asking.

 

Sorry.

 

Okay, I know that you can put PHP inside of HTML.

 

I am wondering if you can completely wrap HTML inside of you PHP?

 

It just seems weird to me to have a ".php" file and yet it is PHP inside of HTML.

 

I dunno.

 

Am I making any more sense?

 

 

 

TomTees

 

 

Link to comment
Share on other sites

To be honest, I'm not sure what you're asking.

 

Sorry.

 

Okay, I know that you can put PHP inside of HTML.

 

I am wondering if you can completely wrap HTML inside of you PHP?

 

It just seems weird to me to have a ".php" file and yet it is PHP inside of HTML.

 

I dunno.

 

Am I making any more sense?

 

I see what you're saying, but you're looking at it the wrong way.  .php files are, well, PHP files.  This means they run on the server.  The PHP tags denote where you want to execute logic.  Code not within those tags is being output to the screen by, essentially, an invisible echo call.  So, something like:

 

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 3</title>
   </head>

   <body>
      <?php
         for($i = 0; $i < 10; ++$i)
         {
            echo $i . "<br />";
         }
      ?>
   </body>

</html>

 

Is actually like:

echo '<!doctype html>\n<html lang="en-us">\n\t<head>\n\t\t<title>\n\t\t\tExample 3\n\t\t</title>\n\t</head>\n\n\t<body>';

for($i = 0; $i < 10; ++$i)
{
   echo '\n\t\t$i<br />';
}

echo '\n\t</body>\n\n</html>';

 

Behind the scenes.

 

The PHP tags tell the PHP interpreter where you want to break from the default, invisible echo and perform logic.  This means that wrapping HTML in PHP tags wouldn't work, as the interpreter treats anything within the tags as language constructs.  You'd get a ton of errors even trying it.

 

Also, just because PHP has tags, that doesn't mean that it has anything to do with HTML's Document Object Model (DOM).  These tags aren't elements.  If you view the rendered source code of a PHP file (like, say, this very forum... right-click, then left-click 'View Source'), you wouldn't see any PHP at all.

Link to comment
Share on other sites

So I don't need a "PHP Header" like you need to define an HTML block?

 

And for my simple registration form, I guess most if not all of my file will be HTML?

 

Is it a sin if you had 100% HTML in a file called MyFile.php?

 

(I was just taught in the past to make all your files ".php" for consistency...)

 

 

TomTees

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.