Page 2 of 2

Posted: Wed Aug 27, 2003 10:50 am
by Nay
JAM wrote: Take this example:

Code: Select all

<?php
A = new email();
B = new email();
?>
[/b]
So it's like, you call the function email() to run with the information store in the variable, which from that example is "new", correct?

and I was also looking at m3rajk's post. He gave the following code for an example:

Code: Select all

function bgnpg ($title){ # BeGiN's each PaGe 
  /* set cookies */ 
  /* set sessions */ 
  /* set other headers */ 
  echo <<<END 
<html> 
  <head> 
  <!-- this is the header for EVERY PAGE OF THE SITE. you have this defined ONCE in ONE file. all changes will affect ALL pages --> 
  <title>$title</title> 
  <!-- meta info --> 
  <!-- java script stuff --> 
  <!-- css stuff --> 
  <!-- other script stuff --> 
  </head> 
  <body bgcolor="#123acd" text="567bef"> 
END; 
}
I noticed the echo <<< END in the code. So it's like "echo everything until END", right? Does <<< mean "until"?

-Nay

Posted: Wed Aug 27, 2003 11:07 am
by jayr517
Nay wrote:
So it's like, you call the function email() to run with the information store in the variable, which from that example is "new", correct?
Nay, not exactly...the "new" is a keyword in PHP (and most other OO languages), to instantiate (or create) a new object of whatever class comes after the "new" command...in this case email().

The Email class I defined above doesn't do anything by itself. It's just a blueprint for any objects of the class I create using the "new" command. Whenever I do something like:

Code: Select all

$A = new email(); 
$B = new email();
I'm creating two new objects from the Email "blueprint". Both $A and $B will have all the variables and functions that I have defined for the Email class. Since $A and $B have multiple variables and functions, you have to have a way of letting PHP know what variable or function you're trying to access, whenever you want to use $A or $B. Hence the ->. Typing $A->subject will give me the subject of the $A object, and typing $B->subject will give me the subject of the $B object.

Hope that makes sense.

I recommend copying my code from above (the class definition) and playing with it in a script.

Posted: Wed Aug 27, 2003 11:51 am
by JPlush76
You're not going to get an understanding of OOP from a forum thread. I suggest you get a good book on OOP or read through the many oop tutorials on the web.

Posted: Wed Aug 27, 2003 12:44 pm
by McGruff
As JPlush said, it'll start to make sense when you read a few tutorials and try you try writing a few scripts of your own.

-> is used to refer to class objects and properties.

Code: Select all

<?php

// a class is instantiated and the getVar() method is called, returning a value for $var
$ob = new MyClass($arg);
$var = $ob->getVar();

// Inside a class definition, you can refer to class properties and methods with $this->.., for example:

// declare a class property
$this->var = 1;

// a method in the class definition which does something to $this->var
function addOne();
{
    $this->var = $this->var + 1;
}

// call the method within the class definition: 
$this->addOne();


?>

Posted: Wed Aug 27, 2003 1:23 pm
by jayr517
I got a good understanding of OOP from forums...;)

Books help but talking to real people helps too...and I haven't had the time or money to take a bunch of OOP classes at a university.

Posted: Wed Aug 27, 2003 5:43 pm
by Nay
Okay, so I'm playing around with it. I've come up with:

Code: Select all

<?

Class P
{

var $say;

function P_say()
{
echo "<p>";
echo $this->say;
echo "</p>";
}

}

$echo = new P();
$say = $echo->say = "oter";
$say = $echo->P_say();

?>
Well, though you could remove the Class P { and } and yet have it function properly. I just thought I'd try it out using a class. Well, it's good for a first, right? :-D.

From what I understand now. I'll need to use "var" to set a variable within the Class, and to access it, I need to use $this->var.

-Nay

Posted: Wed Aug 27, 2003 7:54 pm
by m3mn0n
Books explain it in such a good way, it's impossible NOT to understand it. =P

If you don't have the time/money to goto a class on the subject or purchase a book on the subject the local library should have a book or two, if not an hour or so at a Bookstore should do ya good.

Some online stuff is good, but they always lack the quality books have. From my experiences anyway.

Posted: Wed Aug 27, 2003 9:12 pm
by trollll
I rather like phpPatterns(), but not necessarily a good starting place. Library = good idea! And if they don't have a book, make them get one for you! They will... Just got a book in today on design patterns I can't wait to start...

Posted: Wed Aug 27, 2003 10:51 pm
by BDKR
trollll wrote: I rather like phpPatterns(), but not necessarily a good starting place. Library = good idea! And if they don't have a book, make them get one for you! They will... Just got a book in today on design patterns I can't wait to start...
PHP Patterns is a good place, but something to realize is that patterns have nothing to do with OOP proper. The patterns are just easier to conceptualize and implement via the use of objects.

So,....

1) You have OOP, which is programming that surrounds the use of objects.

2) The there is OOD, which stands for Object Oriented Design, which is a design approach based on the use of objects proper or otherwise.

3) RDD (Responsibility Driven Design). Based on the idea that any program of reasonable size and up can be broken down into areas of responsibility.

4) Functional programming, which is something that I'm not about to even begin to start talking about as I don't have a good enough understanding. So from this point forward, we'll consider it dropped.

So with 2 and 3 above, we have methodologies that are centered around design. The most important thing to realize with these two methodologies is that on the conceptual level, the actual use of objects is not required.

That may sound strange, but it's true. The reason I mention it is becuase while we can learn the mechanics of OOP in one language or another, we are not yet adept at creating practical solutions with it. OOP by itself doesn't teach good design. It just provides some tools with which good design is more easily realized. That is a very important distinction to make.

I'll reword that. OOP is a programming paradigm. OOD and RDD are design methodologies. Patterns, I would have to say, sit at a level above OOD and RDD.

So let's not blur that line. Doing so is like the concept of making the language simpler in the book 1984. Yeah sure, it may make some sense on the surface as there are fewer words to understand, but it ultimately has a negative effect on the ability of the populace to convey subtleties.

Cheers,
BDKR

Posted: Wed Aug 27, 2003 11:02 pm
by JPlush76

Code: Select all

<?php

echo 'Hello World';
?>

thats all you need to get by in life, just show a client you can print hello world and the world is yours!


:lol: