NameSilo

[Resolved] Php classes

Spaceship Spaceship
Watch

Albino

Munky DesignsEstablished Member
Impact
17
php classes

Hi

ok, say I have two classes. how would I let one class use the others functions?

I can extend the class to inherit functions, but I might have 2 or more I want to inherit.

for instance:

PHP:
class security{

    function strip($name){
        return trim($name);
    }

}

class site{

    function checkName($name){
        $security->strip($name);
        //Check if name available
    }

}

$site = new site();
$site->checkName("Todd");

I hope that makes sense! I've asked on other forums, but never got a clear answer. hopefully you guys can enlighten me :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
This really depends. If the classes are related in some way then the child class could use extends. In your example you would have:-

PHP:
<?php
class security{

    function strip($name){
        return trim($name);
    }

}

class site extends security{

    function checkName($name){
        $this->strip($name);
        //Check if name available
    }

}

$site = new site();
$site->checkName("Todd  ");
?>

If however they are not related (for example say you wanted to submit an sql query) you should initiate the db class within the other and use it that way (or pass a reference into the class so you can use it).
 
0
•••
peter@flexiwebhost said:
This really depends. If the classes are related in some way then the child class could use extends. In your example you would have:-

PHP:
<?php
class site extends security{

    function strip($name){
        return trim($name);
    }

}

class site extends security{

    function checkName($name){
        $this->strip($name);
        //Check if name available
    }

}

$site = new site();
$site->checkName("Todd  ");
?>

If however they are not related (for example say you wanted to submit an sql query) you should initiate the db class within the other and use it that way (or pass a reference into the class so you can use it).
:bingo:

or when using a method of the parent class, you can also call it like this:

PHP:
class site extends security{

    function checkName($name){
        parent::strip($name);
        //Check if name available
    }

}

Btw, www.php.net/manual/en/language.oop5.php is a good read.
 
Last edited:
0
•••
peter@flexiwebhost said:
If however they are not related (for example say you wanted to submit an sql query) you should initiate the db class within the other and use it that way (or pass a reference into the class so you can use it).

ok, this way is best (then I can use two).

so would i tried this:

PHP:
 <?php
class security{

    function strip($name){
        return trim($name);
    }

}

class site{
    
   $security = new security();
	
    function checkName($name){
        $security->strip($name);
        return $name;
    }

}

$site = new site();
echo "'".$site->checkName("Todd  ")"'";
?>

but I get this error:

Code:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/mdesigns/public_html/ScriptsNew/test.php on line 12

I guess i'm doing it wrong :P

EDIT: at the moment it seem I like you both too much to give you any rep :(
I'll sort something out :)
 
0
•••
you forgot the concatenation after $site->checkName("Todd ") the line should read:-

PHP:
echo "'".$site->checkName("Todd  ")."'";

Also I notice a few other errors the complete working script should be:-

PHP:
<?php
class security{

    function strip($name){
        return trim($name);
    }

}

class site
{
   
	var $security;
    function site()
    {
    	$this->security = new security();
    }
    function checkName($name){
        $name = $this->security->strip($name);
        return $name;
    }

}

$site = new site();
echo "'".$site->checkName("Todd  ")."'";
?>
 
Last edited:
0
•••
peter@flexiwebhost said:
you forgot the concatenation after $site->checkName("Todd ") the line should read:-

PHP:
echo "'".$site->checkName("Todd  ")."'";

Also I notice a few other errors the complete working script should be:-

PHP:
<?php
class security{

    function strip($name){
        return trim($name);
    }

}

class site
{
   
	var $security;
    function site()
    {
    	$this->security = new security();
    }
    function checkName($name){
        $name = $this->security->strip($name);
        return $name;
    }

}

$site = new site();
echo "'".$site->checkName("Todd  ")."'";
?>

haha, trust me to make an error :(

cheers, that works!

im sure I'll mess it up and ask a few more questions, but thanks so far :)
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back