Beginner PHP OOP Questions (Full Version)

All Forums >> [Web Development] >> General Web Development



Message


lsfphelpls -> Beginner PHP OOP Questions (3/30/2008 16:18:35)

PHP OOP Q:
vars such
private $var1; // are valid , or must get a start value ?

Inside a class function vars can declared with or without private/public ? WHAT IS THE DIFFERENCE ?
public function getSum() {
private $var1;
$var2=7;
.....
.....
} // end method

This is correct SQL Statement:
private $TableName="reserv";
private $first="Sam";
private $last="Smith";
public function getSum() {
$SQLstring="INSERT INTO $this->TableName VALUES(NULL,'$this->first','$this->last')";
.....
.....
} // end method

When I use an object in a subpage the corresponding class of this object must be attached in a php file or be in the <head> ? May ONLY have this class in the homepage ?




Ryokotsusai -> RE: Beginner PHP OOP Questions (3/31/2008 0:48:04)

quote:

vars such
private $var1; // are valid , or must get a start value ?


Yes you can declare them with or without a starting value:
public $var; // is valid
public $var2 = 'value'; // is valid


quote:

Inside a class function vars can declared with or without private/public ? WHAT IS THE DIFFERENCE ?
public function getSum() {
private $var1;
$var2=7;


to declare a variable, you must use public, private, or protected. To create a function you can use the same declarations, or you can leave them out and it will assume public.

public function func1() {}

// is exactly the same as

function func1() {}


as for the different types of declarations,

public: can be used outside the class ie:
class test {

public $var = "Test";

}

$class = new test;
echo $class->$var; //will echo "Test"


private / protected: I do not remember what the difference is between these 2, but using these will prevent that variable or function from being called outside of the class.

Hope that helps some...




lsfphelpls -> RE: Beginner PHP OOP Questions (3/31/2008 2:32:04)

SQL statement is correct ?

Inside a class function, vars can declared with private/public or ONLY without ?
public function getSum() {
private $var1; // wrong ?
$var2=7; // correct ?
.....
.....
} // end method

When I use an object in a subpage the corresponding class of this object must be attached in a php file or be in the <head> ? May ONLY have this class in the homepage and access object of this class from the subpage ? in other words every time I use[in a file] $object= new className(); the class must exist in this file ?




Ryokotsusai -> RE: Beginner PHP OOP Questions (3/31/2008 4:32:17)

quote:

SQL statement is correct ?


If you are refering to this one:

quote:

$SQLstring="INSERT INTO $this->TableName VALUES(NULL,'$this->first','$this->last')";


then, it looks fine to me, that is how you would call those variables.

quote:


public function getSum() {
private $var1; // wrong ?
$var2=7; // correct ?
.....
.....
} // end method


inside of a method, $var2=7, would be correct

the class needs to be in a connected page or in the same page, ie:

class.php
<?php

class test {

function test_function() {
}
 
}

?>

index.php
<?php
include 'class.php';

$class = new test;
?>

would work fine




lsfphelpls -> RE: Beginner PHP OOP Questions (4/21/2008 11:05:55)

answer asap this:
i)in js in a for loop the counter to increment one by one must be ...,++j;) or ...,j++;)
ii)null value considered and the value="" ?
iii)to set css text color to Transparent what to do ?




alihammad -> RE: Beginner PHP OOP Questions (6/3/2008 2:47:01)

Please tell me how to access global variables in a class..
Like if i want to access form variables through $_POST or $_GET .?
i did this and didnt work

class declaration

$form= new formcheck();
$form->printvar();

class definition

class formcheck()
{
protected $a;

function _construct()
{
$this->a= $_POST['name']
}
function printvar()
{
echo $this->a;

}

}




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.1099854