类与PHP_PHP

Jun 01, 2016 pm 12:39 PM
and class of the variables

Classes and PHP



Rod Kreisler



The hardest concept I
've tried to understand since beginning to use PHP was that of classes. I'd never used a database engine but learning to use MySQL, at least for the more basic functions, was a breeze. Having never used OOP before, classes were novel as well, but understanding the theory and why it was useful escaped me. I knew they must be powerful as "everything" is programmed using OOP, but for the life of me, although I thought I understood the mechanics, I couldn't see the usefulness. Then, just a few days ago, while trying to figure out how to do something with regular functions it hit me just how doing it with objects would make my job much simpler! I'm going to try to explain about them in plain English and hopefully help others like myself.



Classes are nothing more than a collection of variables
and functions acting on those variables. They provide a means of thinking about things in real world terms. In other words they describe an object. An object, or instance, of a class is an actual "living, breathing" structure of that class. Let's say we want to describe a bicycle. A proper class of a bicycle might have the variables $pedals, $chain, $front wheel, $rear wheel, $brakes, and $handle_bars. Functions of the bicycle would include Stop(), Accelerate(), Coast(), TurnLeft() and TurnRight(). You can think of your script as the entity operating that bike. The function Accelerate() could be passed an argument such as $Braking_Force and use that information along with the defined instance variables (probably $brakes and $wheels) and output some result back to your script.



Interesting, but couldn'
t all this be accomplished using regular variables and functions? Yes it could, and if you only had one bike in your script it probably wouldn't make much sense to define a class just for it. But what if you needed several bicycles? It could become quite complex keeping track of all those variables and making sure you pass the correct variables to the different functions, and using objects cuts down on the number of variables you need to pass because the function automatically has available to it all the variables describing the object the function is acting upon. Also, class definitions are easily included in different scripts and you'll be assured that a bicycle works the same way in each script!



Let
's create a class that I actually use on almost every page on my site and you might find useful, too.



I don'
t know about you, but when I'm writing a dynamic web page I hate to have to stop thinking about the logical flow to worry about properly formatting my HTML. As a consequence of this, I often end up with not so attractive pages because I don't want to worry about font faces and sizes, or background and text colors. The solution: using a PHP class to set HTML output attributes with functions to format the text!



I call the
class "Style". It contains the following variables that set important HTML attributes for formatting the output:






class Style {



   
var $text;

   
var $alink;

   
var $vlink;

   
var $link;

   
var $bgcol;

   
var $face;

   
var $size;

   
var $align;

   
var $valign;



}



?>



I
'm sure you're familiar with HTML so the variables should be self- explanatory. Next I created a function for Style called Style:






class Style {



function Style ($text="#000000",$alink="#AA00AA",

$vlink
="#AA00AA",$link="#3333FF",

$bgcol
="#999999",$face="Book Antiqua",$size=3,$align="CENTER",$valign="TOP") {



    $this->text=$text;

    $this->alink=$alink;

    $this->vlink=$vlink;

    $this->link=$link;

    $this->bgcol=$bgcol;

    $this->face=$face;

    $this->size=$size;

    $this->align=$align;

    $this->valign=$valign;



}



}

?>

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Python でクラスとメソッドを使用する方法 Python でクラスとメソッドを使用する方法 Apr 21, 2023 pm 02:28 PM

クラスとメソッドの概念とインスタンス クラス (Class): 同じプロパティとメソッドを持つオブジェクトのコレクションを記述するために使用されます。コレクション内のすべてのオブジェクトに共通のプロパティとメソッドを定義します。オブジェクトはクラスのインスタンスです。メソッド: クラスで定義された関数。クラス構築メソッド __init__(): クラスには init() という名前の特別なメソッド (構築メソッド) があり、クラスがインスタンス化されるときに自動的に呼び出されます。インスタンス変数: クラスの宣言において、属性を変数で表現したものをインスタンス変数と呼びます。インスタンス変数とは、self で修飾された変数です。インスタンス化: クラスのインスタンス、クラスの特定のオブジェクトを作成します。継承: つまり、派生クラス (派生クラス) は基本クラス (ベースクラス) を継承します。

2か月後、人型ロボットWalker Sが服をたたむことができるようになった 2か月後、人型ロボットWalker Sが服をたたむことができるようになった Apr 03, 2024 am 08:01 AM

Machine Power Report 編集者: Wu Xin 国内版の人型ロボット + 大型模型チームは、衣服を折りたたむなどの複雑で柔軟な素材の操作タスクを初めて完了しました。 OpenAIのマルチモーダル大規模モデルを統合したFigure01の公開により、国内同業者の関連動向が注目を集めている。つい昨日、中国の「ヒューマノイドロボットのナンバーワン株」であるUBTECHは、Baidu Wenxinの大型モデルと深く統合されたヒューマノイドロボットWalkerSの最初のデモを公開し、いくつかの興味深い新機能を示した。 Baidu Wenxin の大規模モデル機能の恩恵を受けた WalkerS は次のようになります。 Figure01 と同様に、WalkerS は動き回るのではなく、机の後ろに立って一連のタスクを完了します。人間の命令に従って服をたたむことができる

jQueryを使用して要素のクラス名を置換する jQueryを使用して要素のクラス名を置換する Feb 24, 2024 pm 11:03 PM

jQuery は、Web 開発で広く使用されている古典的な JavaScript ライブラリで、イベントの処理、DOM 要素の操作、Web ページ上でのアニメーションの実行などの操作を簡素化します。 jQueryを使っていると要素のクラス名を置き換える場面がよくありますが、この記事ではその実践的な方法と具体的なコード例を紹介します。 1.removeClass() メソッドと addClass() メソッドを使用する jQuery には、削除用の RemoveClass() メソッドが用意されています。

Pythonにおけるクラスとは何を意味するのでしょうか? Pythonにおけるクラスとは何を意味するのでしょうか? May 21, 2019 pm 05:10 PM

クラスとはPythonでクラスを定義するためのキーワードです クラスの定義方法:classの後ろにスペースを入れてからクラス名を追加します; クラス名のルール:最初の文字を大文字にします 複数の単語がある場合はキャメルケースを使用してください[class Dog()] などの名前付け。

PHP クラスの使用法の詳細な説明: コードをより明確で読みやすくします。 PHP クラスの使用法の詳細な説明: コードをより明確で読みやすくします。 Mar 10, 2024 pm 12:03 PM

PHP コードを記述するとき、クラスを使用するのは非常に一般的な方法です。クラスを使用すると、関連する関数とデータを 1 つのユニットにカプセル化できるため、コードがより明確になり、読みやすく、保守しやすくなります。この記事では、PHPClass の使用法を詳しく紹介し、クラスを適用して実際のプロジェクトでコードを最適化する方法を読者がよりよく理解できるように、具体的なコード例を示します。 1. クラスの作成と使用 PHP では、キーワード class を使用してクラスを定義し、クラス内のプロパティとメソッドを定義できます。

Vue エラー: v-bind を使用してクラスとスタイルを正しくバインドできません。解決方法は? Vue エラー: v-bind を使用してクラスとスタイルを正しくバインドできません。解決方法は? Aug 26, 2023 pm 10:58 PM

Vue エラー: v-bind を使用してクラスとスタイルを正しくバインドできません。解決方法は? Vue 開発では、v-bind 命令を使用してクラスとスタイルを動的にバインドすることがよくありますが、v-bind を正しく使用してクラスとスタイルをバインドできないなどの問題が発生することがあります。この記事では、この問題の原因と解決策を説明します。まず、v-bind ディレクティブを理解しましょう。 v-bind は V をバインドするために使用されます

PHP への解決策 注意: 変数のみを参照によって渡す必要があります。 PHP への解決策 注意: 変数のみを参照によって渡す必要があります。 Jun 22, 2023 pm 09:04 PM

PHP は一般的に使用されるサーバー側スクリプト言語であるため、Web サイトを開発するときに PHP Notice エラー メッセージが表示されることがよくあります。その中でも、「PHP Notice:Onlyvariables shouldbepassedbyreferencein」はよくあるエラー メッセージです。このエラー メッセージの意味は、変数のみを参照渡しする必要があるということです。関数またはメソッドでは、パラメーターを渡すことによって変数を呼び出すことができることがわかっています。そのため、コード内では次のようになります。

jqueryで要素にクラスがあるかどうかを確認する方法 jqueryで要素にクラスがあるかどうかを確認する方法 Mar 21, 2023 am 10:47 AM

jquery が要素にクラスがあるかどうかを判断する方法: 1. 「hasClass('classname')」メソッドを通じて要素に特定のクラスがあるかどうかを判断します; 2. 「is('.classname」を通じて要素に特定のクラスがあるかどうかを判断します。 ')" 方法。

See all articles