You are not logged in.
- Topics: Active | Unanswered
- Index
- » Programming
- » Classes
Pages: 1
#1 2009-11-19 02:52:42
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Classes
Finally decided to have a play around with classes, and just wanted to check that this was a standard/safe way to load external vars into classes so that they are available throughout the class. Are there any caveats or suchlike to doing things this way?
<?php
$test[1] = 'Test 1'."\n";
$test[2] = 'Test 2'."\n";
$test[3] = 'Test 3'."\n";
$info[1] = 'Info 1'."\n";
$info[2] = 'Info 2'."\n";
class test_class
{
function test_class($info, $test)
{
$this->info = $info;
$this->test = $test;
}
function printout()
{
print($this->test[1]);
$this->printout2();
$this->printout3();
}
function printout2()
{
print($this->test[2]);
}
}
class test_class2 extends test_class
{
function printout3()
{
print($this->test[3]);
print($this->info[1]);
print($this->info[2]);
}
}
$class = new test_class2($info, $test);
$class->printout();
?>Cheers.
Screw the chavs and God save the Queen!
Offline
#2 2009-11-19 03:35:00
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,135
- Website
Re: Classes
You never declare $this->info or $this->test before using them. You should declare them explicitly within the class definition.
Also, test_class's printout function isn't technically valid: test_class doesn't have a printout3 method.
Last edited by Smartys (2009-11-19 03:35:14)
Offline
#3 2009-11-19 03:58:49
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
You never declare $this->info or $this->test before using them. You should declare them explicitly within the class definition.
How is that done? Is that done by adding:
var $info;
var $test;to the parent class? Apologies if that's a basic question, btw. My mind has started turning to mush trying to get to grips with classes and their specifics. ![]()
Also, test_class's printout function isn't technically valid: test_class doesn't have a printout3 method.
Doesn't it inherit that from the second class, or do the extension classes only inherit from the parent class?
Last edited by MattF (2009-11-19 04:26:59)
Screw the chavs and God save the Queen!
Offline
#4 2009-11-19 04:49:57
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,135
- Website
Re: Classes
1. http://www.php.net/manual/en/language.o … erties.php
2. http://www.php.net/manual/en/language.o … itance.php
Hopefully those should answer your questions.
Offline
#5 2009-11-19 05:05:21
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
Cheers Smartys.
If I've understood those correctly, the following should be valid and correct methods? If I'm still way off, I'm calling it a day for today. ![]()
class test_class1
{
public $info;
public $test;
function test_class1($info, $test)
{
$this->info = $info;
$this->test = $test;
}
function printout1()
{
print($this->test[1]);
}
function printout2()
{
print($this->test[2]);
}
function printout3()
{
print($this->test[3]);
}
}
class test_class2 extends test_class1
{
function printout()
{
$this->printout1();
$this->printout2();
$this->printout3();
print($this->info[1]);
print($this->info[2]);
}
}
$class = new test_class2($info, $test);
$class->printout();Screw the chavs and God save the Queen!
Offline
#6 2009-11-19 06:29:15
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,135
- Website
Re: Classes
Yeah, that would work. In practice, setting all your fields to public is a bad idea (see: http://en.wikipedia.org/wiki/Encapsulat … science%29), but that code is semantically valid.
Offline
#7 2009-11-19 16:49:36
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
Cheers.
I think I'm starting to understand the basics now. Hope you don't mind if I ask a couple more questions, just to check I've got the plot now? ![]()
Is the following code correct/valid, with regards to declaring a global within the class, to save having to pass the var/array directly? I've changed the declarations to protected now, btw. ![]()
class test_class
{
protected $info;
protected $test;
function test_class($test)
{
global $info;
$this->info = $info;
$this->test = $test;
}
function printout()
{
foreach ($this->info as $value)
{
print($value);
}
print_r($this->test);
}
}
$class = new test_class($test);Secondly, you mentioned it's semantically correct. Is it correct in the logical sense? i.e: am I using an arse ways round method or is the logic/method fine? Apologies for asking so many questions, btw. Even using the documentation, it just seems to be a bit weird to get to grips with from a complete standing start. I'm just trying to make sure I'm understanding correctly from the start rather than later down the line.
Thanks again for all of your help Smartys. ![]()
Screw the chavs and God save the Queen!
Offline
#8 2009-11-19 17:43:20
- Reines
- Lead developer

- From: Scotland
- Registered: 2008-05-11
- Posts: 3,165
- Website
Re: Classes
In general you should try avoid the use of global variables as much as possible if you are aiming for an OOP approach, so I would pass $info in as a parameter instead.
Although not required I would also tend to explicitly state the visibility of each function within the class (i.e. "protected function printout()").
Since PHP5 (which I assume you're using) constructors and destructors use the form "function __construct()" and "function __destruct()", instead of the old style "function class_name()". You probably want to get into the habit of using these rather than the old style.
Offline
#9 2009-11-19 18:03:00
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
In general you should try avoid the use of global variables as much as possible if you are aiming for an OOP approach, so I would pass $info in as a parameter instead.
Is that to allow for maximum portability of the code?
Although not required I would also tend to explicitly state the visibility of each function within the class (i.e. "protected function printout()").
To protect the functions which should only be accessed from within the class itself?
Since PHP5 (which I assume you're using) constructors and destructors use the form "function __construct()" and "function __destruct()", instead of the old style "function class_name()". You probably want to get into the habit of using these rather than the old style.
I'll get reading up on those then.
Cheers Reines. ![]()
Screw the chavs and God save the Queen!
Offline
#10 2009-11-19 18:18:47
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,135
- Website
Re: Classes
Is the following code correct/valid, with regards to declaring a global within the class, to save having to pass the var/array directly?
Yes, I believe it's valid. However, I would avoid using globals to the extent that you can (they just don't really fit with the whole OO-paradigm).
Secondly, you mentioned it's semantically correct. Is it correct in the logical sense? i.e: am I using an arse ways round method or is the logic/method fine?
Well, given that it's a test class, I suppose so. To really judge it "in the logical sense" I would need a class with a more logical purpose ![]()
Offline
#11 2009-11-19 18:31:32
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
Well, given that it's a test class, I suppose so. To really judge it "in the logical sense" I would need a class with a more logical purpose
Makes sense.
I was just keeping it small until I'd understood the basics correctly. A slightly more useful version below. The alterations suggested by yourself and Reines have been made and noted for future reference.
It's still a basic thing, with the simple purpose of finding out whether an array is multi-dimensional or not and printing the output accordingly.
class test_class
{
protected $info;
protected $test;
function __construct($info, $test)
{
$this->info = $info;
$this->test = $test;
}
function check_levels()
{
if (!empty($this->info[0]) && is_array($this->info[0]))
{
foreach ($this->info as $key)
{
$this->printout($key);
}
}
else
{
$this->printout($this->info);
}
}
protected function printout($key)
{
foreach ($key as $value)
{
print($value."\n");
}
}
}
$class = new test_class($info, $test);
$class->check_levels();Screw the chavs and God save the Queen!
Offline
#12 2009-11-20 04:53:47
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
To really judge it "in the logical sense" I would need a class with a more logical purpose
This is a preliminary version of what I was intending to play around with. I'm correct in surmising this would allow generate_output($vars) to be called multiple times to generate different output?
class generate_display_output
{
protected $print;
protected $fields;
protected $template;
protected $directory;
protected $separator;
function generate_output($input, $tpl, $dir = false, $prnt = true, $sep = false)
{
$this->print = $prnt;
$this->fields = $input;
$this->template = $tpl;
$this->directory = $dir;
$this->separator = $sep;
if (count(array_keys($this->fields)) > '1')
{
foreach ($this->fields as $block)
{
$this->generate($block);
}
else
{
$this->generate($this->fields);
}
}
}
protected function generate($input)
{
$count = '0';
$basedir = root_dir.'include/templates/';
$find = array(
'#([^\{])\{[\-\d\w]{1,}\}#',
'#(<[\d\w\s"=]{1,}>)[\s\R]?(</[\d\w]{1,}>)#i',
'#(<textarea.+?>)&\#160;(</textarea>)#i',
);
$replace = array('$1', '$1 $2', '$1$2');
if ($this->directory && is_dir($basedir.$this->directory))
{
$tplfile = $basedir.$this->directory.'/'.$this->template;
}
else
{
$tplfile = $basedir.$this->template;
}
if (is_file($tplfile))
{
$output = file_get_contents($tplfile);
$output = run_code_include($output);
foreach ($input as $key => $value)
{
if (is_array($value))
{
if (!$this->separator)
{
$value = implode($value);
}
else
{
$value = implode($this->separator, $value);
}
}
if (utf8_trim($value) != '')
{
$count++;
}
$output = str_replace('{'.$key.'}', $value, $output);
}
$output = preg_replace($find, $replace, $output);
}
if ($count == '0')
{
$output = '';
}
if ($this->print)
{
print($output);
}
else
{
return $output;
}
}
}Screw the chavs and God save the Queen!
Offline
#13 2009-11-20 09:18:18
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 4,071
- Website
Re: Classes
Yes, it's just like a function that can be called multiple times, e.g. with different parameters, except that it has access to class member variables and can change its state based on those for example.
Offline
#14 2009-11-22 23:07:50
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Classes
Cheers.
That's what I was hoping for. I think I've grasped the gist of the basics now.
Screw the chavs and God save the Queen!
Offline
Pages: 1
- Index
- » Programming
- » Classes
