hello,
how can i create new instace of class in other class?
class InputF
{
private $fType;
private $fName;
private $fValue;
private $fWidth;
public function __construct($fType,$fName,$fValue,$fWidth)
{
$this->fType = $fType;
$this->fName = $fName;
$this->fValue = $fValue;
$this->fWidth = $fWidth;
}
public function type()
{
return ' type="'.$this->fType.'" ';
}
public function name()
{
return ' name="'.$this->fName.'" ';
}
public function value()
{
return ' value="'.$this->fValue.'" ';
}
public function width()
{
return ' width="'.$this->fWidth.'" ';
}
public function printF()
{
return '<input '.$this->type().$this->name().$this->value().$this->width().'>';
}
}
how to create instance in another class? for example bellow...
class searchForm
{
public $callInputName = new InputF('text','customer','asdf','300'); //somehow like that?
public $callOkButton = new InputF('submit','submit','OK',null); //somehow like that?
.
.
.
.
}
thank you for your helpYou have to do it in the constructor I believe.
Though before you get too carried away writing yet another way to write html via php I would consider why html was designed the way it was by a lot of intelligent people.
Your constructor for InputF only has $fType,$fName,$fValue,$fWidth. What about class, what about ID, what about style? What about onchange or all the other possible events
<!-- m --><a class="postlink" href="http://www.w3.org/TR/html401/interact/scripts.html">http://www.w3.org/TR/html401/interact/scripts.html</a><!-- m -->
I have dealt with the approach you are trying to do and it always ends in a whole at some point. Reverting to html becomes easier as it is very compact and readable with indentation etc. Ripping this stuff out always led to a large decrease in code and a large increase in readablity.
The pure oop method is dom as it (tries to?) cater for all eventualities. it is a pain to do as one line of html takes a multitude of lines.
Use a templating engine instead and use seperation of code and display.
Sorry if it is a bit terse but I still have heaps of this sort of stuff left over and it has left things unmaintainable except for the person who wrote it when they are really on the ball. Each language was designed, whether it be html,sql,php, to do things in a readable efficient fashion for the domain it is aimed at.doesn't the code your using work? it looks ok to me.
make sure the files are all included (unless you are using __autoload)class searchForm
{
public $callInputName;
function __construct()
{
$this->callInputName = new InputF('text','customer','asdf','300');
}
}You have to do it in the constructor I believe.
Though before you get too carried away writing yet another way to write html via php I would consider why html was designed the way it was by a lot of intelligent people.
Your constructor for InputF only has $fType,$fName,$fValue,$fWidth. What about class, what about ID, what about style? What about onchange or all the other possible events
...oh, yes I fully agree with you,but I have started to "play" with classes only today due to study java first year and I like idea of object oriented languages and php does support of classes.
for example:
this form i wanted to use for search engine on my webiste, but I use search field and button "OK" in more windows, but on each site it takes different arguments, and I write all the time almost same code with little differences. I just wanted to try if it will work. I did few project, which are not really big but not too small either. And after some time I m confused in my code. So I wanted to write class for as many classes as possible; for example spare for mysql queries or for different types of forms... And if I needed more arguments for input element, I would just simply add it there...
...but, I had same question today, as Is your answer and I will see, if my style of coding is ok for me or not. but I don't know myself yet.
PS. sorry for my english Java allows you to assign on creation of a member object like you were trying to though PHP doesn't..
Inputs are pretty simple as they are childless elements in that they not have any children like tables, divs, spans etc have. In firefox you can use dom inspector to see all the objects created and the levels of those objects.
The thing you jave to remember with html everthing is an object, the html is just a markup language for the creation of objects by the browser code. It's actually very very good because in pretty things a lot of those objects are just throw away containers for positioning, colouring etc. Also why probably a lot of apps are a lot blander than web sites. People do try and go the other way in Java with things like XUI <!-- m --><a class="postlink" href="http://www.xoetrope.com/zone/manual/index.php?zone=XUI">http://www.xoetrope.com/zone/manual/index.php?zone=XUI</a><!-- m --> so they can use an XML markup language to write the java as Swing does lead you to thinking I could do this in one line of html and it's taken me 7 in swing.
Don't worry about your english What you are after is the Factory Pattern (<!-- m --><a class="postlink" href="http://www.phptr.com/articles/article.asp?p=101193&seqNum=3&rl=1">http://www.phptr.com/articles/article.a ... Num=3&rl=1</a><!-- m -->) sometimes also called the Factory Method (<!-- m --><a class="postlink" href="http://www.phppatterns.com/docs/design/the_factory_method">http://www.phppatterns.com/docs/design/ ... ory_method</a><!-- m -->). The best tute for this that I know of is by Alejandro Gervasio part 1 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/The-Basics-of-Abstract-Factory-Classes-in-PHP-5/">http://www.devshed.com/c/a/PHP/The-Basi ... -in-PHP-5/</a><!-- m -->), part 2 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/Creating-AJAX-Requester-Objects-with-Abstract-Factory-Classes-in-PHP-5/">http://www.devshed.com/c/a/PHP/Creating ... -in-PHP-5/</a><!-- m -->), part 3 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/Using-Abstract-Factory-Classes-in-PHP-5-to-Work-with-Online-Forms/">http://www.devshed.com/c/a/PHP/Using-Ab ... ine-Forms/</a><!-- m -->). Once you have created your abstract factory class and your concrete element classes you will be able to reuse them again and again as you wish.
how can i create new instace of class in other class?
class InputF
{
private $fType;
private $fName;
private $fValue;
private $fWidth;
public function __construct($fType,$fName,$fValue,$fWidth)
{
$this->fType = $fType;
$this->fName = $fName;
$this->fValue = $fValue;
$this->fWidth = $fWidth;
}
public function type()
{
return ' type="'.$this->fType.'" ';
}
public function name()
{
return ' name="'.$this->fName.'" ';
}
public function value()
{
return ' value="'.$this->fValue.'" ';
}
public function width()
{
return ' width="'.$this->fWidth.'" ';
}
public function printF()
{
return '<input '.$this->type().$this->name().$this->value().$this->width().'>';
}
}
how to create instance in another class? for example bellow...
class searchForm
{
public $callInputName = new InputF('text','customer','asdf','300'); //somehow like that?
public $callOkButton = new InputF('submit','submit','OK',null); //somehow like that?
.
.
.
.
}
thank you for your helpYou have to do it in the constructor I believe.
Though before you get too carried away writing yet another way to write html via php I would consider why html was designed the way it was by a lot of intelligent people.
Your constructor for InputF only has $fType,$fName,$fValue,$fWidth. What about class, what about ID, what about style? What about onchange or all the other possible events
<!-- m --><a class="postlink" href="http://www.w3.org/TR/html401/interact/scripts.html">http://www.w3.org/TR/html401/interact/scripts.html</a><!-- m -->
I have dealt with the approach you are trying to do and it always ends in a whole at some point. Reverting to html becomes easier as it is very compact and readable with indentation etc. Ripping this stuff out always led to a large decrease in code and a large increase in readablity.
The pure oop method is dom as it (tries to?) cater for all eventualities. it is a pain to do as one line of html takes a multitude of lines.
Use a templating engine instead and use seperation of code and display.
Sorry if it is a bit terse but I still have heaps of this sort of stuff left over and it has left things unmaintainable except for the person who wrote it when they are really on the ball. Each language was designed, whether it be html,sql,php, to do things in a readable efficient fashion for the domain it is aimed at.doesn't the code your using work? it looks ok to me.
make sure the files are all included (unless you are using __autoload)class searchForm
{
public $callInputName;
function __construct()
{
$this->callInputName = new InputF('text','customer','asdf','300');
}
}You have to do it in the constructor I believe.
Though before you get too carried away writing yet another way to write html via php I would consider why html was designed the way it was by a lot of intelligent people.
Your constructor for InputF only has $fType,$fName,$fValue,$fWidth. What about class, what about ID, what about style? What about onchange or all the other possible events
...oh, yes I fully agree with you,but I have started to "play" with classes only today due to study java first year and I like idea of object oriented languages and php does support of classes.
for example:
this form i wanted to use for search engine on my webiste, but I use search field and button "OK" in more windows, but on each site it takes different arguments, and I write all the time almost same code with little differences. I just wanted to try if it will work. I did few project, which are not really big but not too small either. And after some time I m confused in my code. So I wanted to write class for as many classes as possible; for example spare for mysql queries or for different types of forms... And if I needed more arguments for input element, I would just simply add it there...
...but, I had same question today, as Is your answer and I will see, if my style of coding is ok for me or not. but I don't know myself yet.
PS. sorry for my english Java allows you to assign on creation of a member object like you were trying to though PHP doesn't..
Inputs are pretty simple as they are childless elements in that they not have any children like tables, divs, spans etc have. In firefox you can use dom inspector to see all the objects created and the levels of those objects.
The thing you jave to remember with html everthing is an object, the html is just a markup language for the creation of objects by the browser code. It's actually very very good because in pretty things a lot of those objects are just throw away containers for positioning, colouring etc. Also why probably a lot of apps are a lot blander than web sites. People do try and go the other way in Java with things like XUI <!-- m --><a class="postlink" href="http://www.xoetrope.com/zone/manual/index.php?zone=XUI">http://www.xoetrope.com/zone/manual/index.php?zone=XUI</a><!-- m --> so they can use an XML markup language to write the java as Swing does lead you to thinking I could do this in one line of html and it's taken me 7 in swing.
Don't worry about your english What you are after is the Factory Pattern (<!-- m --><a class="postlink" href="http://www.phptr.com/articles/article.asp?p=101193&seqNum=3&rl=1">http://www.phptr.com/articles/article.a ... Num=3&rl=1</a><!-- m -->) sometimes also called the Factory Method (<!-- m --><a class="postlink" href="http://www.phppatterns.com/docs/design/the_factory_method">http://www.phppatterns.com/docs/design/ ... ory_method</a><!-- m -->). The best tute for this that I know of is by Alejandro Gervasio part 1 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/The-Basics-of-Abstract-Factory-Classes-in-PHP-5/">http://www.devshed.com/c/a/PHP/The-Basi ... -in-PHP-5/</a><!-- m -->), part 2 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/Creating-AJAX-Requester-Objects-with-Abstract-Factory-Classes-in-PHP-5/">http://www.devshed.com/c/a/PHP/Creating ... -in-PHP-5/</a><!-- m -->), part 3 (<!-- m --><a class="postlink" href="http://www.devshed.com/c/a/PHP/Using-Abstract-Factory-Classes-in-PHP-5-to-Work-with-Online-Forms/">http://www.devshed.com/c/a/PHP/Using-Ab ... ine-Forms/</a><!-- m -->). Once you have created your abstract factory class and your concrete element classes you will be able to reuse them again and again as you wish.